Im Getting System.datetime.now
from different Machine .Each system having different datetime format as a mension below
16-Oct-12 7:25:22 PM
16/10/2012 7:10:47 PM [DD/MM/YYYY]
10/16/2012 7:10:51 PM [MM/DD/YYYY]
How To convert Different format of DateTime
to specific String format ?
string sDateTime = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt");
The problem of your example is the last two formats
16/10/2012 7:10:47 PM [DD/MM/YYYY]
and10/16/2012 7:10:51 PM [MM/DD/YYYY]
. If the value is10/11/2012 7:20:10 PM
how can you know that it is Oct 11, 2012 or Nov 10, 2012?As you can see,
"16/10/2012 7:10:47 PM"
could not be parse.First result of Google search: Custom datetime format strings from MSDN
You have to use the line of code you provided explicitly on the other machines when returning the datetime:
This is however not aware of time zones! If your application spans several time zones, you should take that into count too!
Or, even better, you could return the Unix time (milliseconds from 1970-01-01) as a long, and of course the timezone info too, if that is different across the machines... (Beware, .NET expoch time is from 0001-01-01 though!)
Returning Epoch time
[DD/MM/YYYY]
or[MM/DD/YYYY]
, only when the month of day is greater than 12... And this would lead to mysterious errors. Believe me, been there, done that (the debugging part)...try the TryParse methode of the Datetime class
Examples from the link:
Notice that in the example its not working for all given strings dates