0

I want to convert date to a specific format (e.g. en-US MM/dd/yyyy) and I am aware about normal method to parse it.

But in my case I'm unaware about the source date format. Source date format is subject to change as per server environment. It can be en-US or en-GB.

e.g. DateTime dt = DateTime.Now;

'dt' can be '27/03/2014' or '03/27/2014'.

How to convert the source date to en-US format if I don't know source date format?

(string format would be fine - MM/dd/yyyy e.g. "03/27/2014").

nirav
  • 373
  • 3
  • 7
  • 20
  • I'm confused...in your example you already have a datetime object so the "source format" isn't important. Have I misunderstood? – DoctorMick Mar 27 '14 at 11:48
  • I have a DateTime object. True. But according to host environment, it returns date. Converting any date format to MM/dd/yyyy will create problem if it already MM/dd/yyyy. – nirav Mar 27 '14 at 11:54
  • 2
    You can't. If you have 01/02/2014 depending its 1 February or 2 January. You have to know how to read it – wiero Mar 27 '14 at 11:54
  • Why would it cause a problem? A date is a date, it knows internally what day, month and year it is so if you just need to format the DateTime object you can just use .ToString with whatever format you need. – DoctorMick Mar 27 '14 at 11:59
  • @DoctorMick it would create problem. Refer wiero's comment. – nirav Mar 27 '14 at 12:05
  • 1
    @nirav: you're confusing the DateTime object type (which does not have ambiguity) with string representation of date/time (which does have it). – DarkWanderer Mar 27 '14 at 12:09
  • Possible Duplicate: http://stackoverflow.com/questions/3707485/how-to-convert-string-to-date-without-knowing-the-format – neel shah Mar 27 '14 at 12:15
  • @DarkWanderer you're right. Just needed to convert it to the specific format using `ToString("MM/dd/yyyy")`. `Parse` is not working. But 'ToString()' method is surely the way. Thanks for pointing out. e.g. `dt.Tostring("MM/dd/yyyy");` – nirav Mar 27 '14 at 12:20

6 Answers6

1

If you don't know the source format, there is a chance of getting errors while trying to convert. For example, try converting:

05/01/2013

A computer wouldn't be able to identify the date in such a case. It could result in two outputs: 05 Jan, 2013 or 01 May, 2013.

neel shah
  • 2,231
  • 15
  • 19
  • exactly. I'm facing this issue currently. Even it can throw error if you take date like 15/05/2013. – nirav Mar 27 '14 at 11:55
  • @nirav you could send a specific string: `d:05/m:01/y:2013`. Then converting it only needs to be application specific. – Re Captcha Mar 27 '14 at 12:05
  • @ReCaptcha in this case I don't know the date. Date is fetched dynamically. Format too is unknown. – nirav Mar 27 '14 at 12:10
0
DateTime.Now.toString("yyyy-MM-dd"); //toString(specify format)

try this one

Mohini Mhetre
  • 912
  • 10
  • 29
  • You're right as I wanted date in string format only. Though I have tried to give more explanation in my Answer. Thank You. – nirav Mar 27 '14 at 12:37
0
DateTime result;
if (!DateTime.TryParseExact(inputString, "dd/MM/yyyy", out result)
    result = DateTime.ParseExact(inputString, "MM/dd/yyyy");

OR

DateTime result;
if (!DateTime.TryParse(inputString, out result)
    result = DateTime.ParseExact(inputString, CultureInfo.InvariantCulture, DateTimeStyles.None);
Alex
  • 172
  • 1
  • 6
0

If you know your environment will always be the deciding factor, why not just use that?

Try some variation of the following:

string yourFormat = ... // Whatever is your default format

if(Thread.CurrentThread.CurrentCulture.Name == "en-US")
{
     yourFormat = "MM/dd/yyyy";
}
else (if Thread.CurrentThread.CurrentCulture.Name == "en-GB")
{
     yourFormat = "dd/MM/yyyy";
}

DateTime d = DateTime.ParseExact(inputString, yourFormat, null)
Kjartan
  • 18,591
  • 15
  • 71
  • 96
0

How to convert the source date to en-US format if I don't know source date format?

You need to know the source date format, only then can you convert it to the required date format.

As wiero has rightly said in the comments "If you have 01/02/2014 depending its 1 February or 2 January. You have to know how to read it".

The default format of the object returned by DateTime.Now will be the one specified in your server setting, check the screenshot below: enter image description here

enter image description here

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
0

Referring to @DarkWanderer's comment in question:

DateTime object has nothing to do with format.

Just needed to convert it to the specific format using ToString("MM/dd/yyyy").

I was using Parse() method to convert but it will not work. BToString() method is surely a way.

This will work: dt.Tostring("MM/dd/yyyy");

Thanks @DarkWanderer.

nirav
  • 373
  • 3
  • 7
  • 20