Reputation: 10723
I have this:
DateTime date = new DateTime();
and i then print it. Here are the results:
Localhost:
date: 1/1/0001 12:00:00 AM
On server:
date: 1-1-0001 0:00:00
The correct way is the one on localhost. How to fix this and why is this happening? It's the same code.
Upvotes: 0
Views: 248
Reputation: 62544
Because of different regional setting on different machines. To have date time output in the same format you ahve to specify format string explciitly:
date.ToString("yyyy-MM-dd HH:mm:ss");
Also as John recommeded in comments below if you want having date time output in the same format on different machines despite local regional settings you can use InvariantCulture format provider:
date.ToString(CultureInfo.InvariantCulture);
MSDN:
The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region
MSDN:
Upvotes: 5
Reputation: 101748
To display the date the way you've shown it there, you could use:
date.ToString("M/d/yyyy hh:mm:ss tt");
Or when using a format string:
string.Format("date: {0:M/d/yyyy hh:mm:ss tt}", date);
Upvotes: 0
Reputation: 7463
You can use the invariant culture to display your date, if you are not too concerned about the format so long as it is always the same.
date.ToString(System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat);
Upvotes: 0