0

I am receiving some data into a variable of type object. In certain cases this data are date values. For that data, I would like to convert this to a string and return it in the same format as it was passed. In some cases, the object could be a datetime, in others a date only or time only values.

As soon as I convert the object to a date or a string, it is obviously given a time of midnight which in my scenario may be a valid time (so I cannot test to see if the time is midnight in which case I could deduce that it would have been a date only date value, nor can I use regex on it as there will always be a time element).

Intellisense shows me it correctly, ie in the format I am wishing to return the value.

Is there an easy way to achieve this (hopefully without using reflection)

Many thx

Simon

1
  • Add some examples of in and output. You're question is pretty generic. Dec 8, 2011 at 12:50

3 Answers 3

0

Your question is a little unclear but I think you're looking for something like this:

DateTime result;

if (DateTime.TryParse(value, out result))
{
    // use result here
}

In the above code value is a string that represents the data coming in. The code will only enter the if block if the string is a valid DateTime. At which point you can do the processing you need on it.

0

Im not sure i understand the question but i would recommend you to take a look at this conversion example on MSDN, and see the Documentation of the DateTime Structur it contains a lot of Conversion/Formatting Methods i hope it helps.

0

There are many way to do formatting on the datetime and one of the simple way is fetch the data from the required table in the desired format. Like here you need to display the date and if you your format is dd/MM/yyyy then try this

select Convert(varchar(10),StartDate,103) as StartDateformat from table where filtername=@filtername

use this link to find other format Cast and Convert

From local variable to DateTime Conversion

DateTime todateformat = Convert.ToDateTime(txttodate.Text.Trim());

From DateTime to local variable Conversion in specific format

string startdate = todateformat.ToString("dd/MM/yyyy hh:mm:ss");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.