0

I came across an issue that I'm not sure how to resolve. I developed a web application which makes use of DateTime.UtcNow. After testing on my local machine and everything worked fine, I deployed the application to the server. The server is flipping the month and day, so instead of it being August 10th, it says it is October 8th. This must be a regional thing, but I don't want to force a specific format. What's the best solution for this?

EDIT: I think it might be because I'm converting the datetimes into strings. How should I represent them as a string depending on the machine?

Jon Martin
  • 3,252
  • 5
  • 29
  • 45

3 Answers3

3

When displaying the DateTime, format it with a specific culture.

myDateTime.ToString(CultureInfo.InvariantCulture);
myDateTime.ToString(new CultureInfo("en-GB"));
myDateTime.ToString(new CultureInfo("en-US"));

I also suggest reading up on standard and custom date and time format strings.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • CultureInfo.InvariantCulture: will this give the right time in all regions then? – Jon Martin Aug 10 '11 at 13:55
  • @Jon - It stands for "no culture" and is based on `en-US`. So, in the UK it will not be correct. – Oded Aug 10 '11 at 13:56
  • Thanks, makes sense. So what would happen if a user passed in a US time to the server, which is in Canada? – Jon Martin Aug 10 '11 at 14:07
  • @Jon - If you format the `DateTime` using `InvariantCulture`, it will show up the same way _everywhere_. – Oded Aug 10 '11 at 14:19
0

you can use the ISO-Fromat for reading and saving dates

have a look at the below:

Convert dateTime to ISO format yyyy-mm-dd hh:mm:ss in C#

Community
  • 1
  • 1
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
0

The MSDN has a really good example:

using System;

public class DateToStringExample
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2008, 6, 15, 21, 15, 07);
      // Create an array of standard format strings.
      string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o", 
                               "R", "s", "t", "T", "u", "U", "y"};
      // Output date and time using each standard format string.
      foreach (string standardFmt in standardFmts)
         Console.WriteLine("{0}: {1}", standardFmt, 
                           dateValue.ToString(standardFmt));
      Console.WriteLine();

      // Create an array of some custom format strings.
      string[] customFmts = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f", 
                             "dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };
      // Output date and time using each custom format string.
      foreach (string customFmt in customFmts)
         Console.WriteLine("'{0}': {1}", customFmt,
                           dateValue.ToString(customFmt));
   }
}
// This example displays the following output to the console:
//       d: 6/15/2008
//       D: Sunday, June 15, 2008
//       f: Sunday, June 15, 2008 9:15 PM
//       F: Sunday, June 15, 2008 9:15:07 PM
//       g: 6/15/2008 9:15 PM
//       G: 6/15/2008 9:15:07 PM
//       m: June 15
//       o: 2008-06-15T21:15:07.0000000
//       R: Sun, 15 Jun 2008 21:15:07 GMT
//       s: 2008-06-15T21:15:07
//       t: 9:15 PM
//       T: 9:15:07 PM
//       u: 2008-06-15 21:15:07Z
//       U: Monday, June 16, 2008 4:15:07 AM
//       y: June, 2008
//       
//       'h:mm:ss.ff t': 9:15:07.00 P
//       'd MMM yyyy': 15 Jun 2008
//       'HH:mm:ss.f': 21:15:07.0
//       'dd MMM HH:mm:ss': 15 Jun 21:15:07
//       '\Mon\t\h\: M': Month: 6
//       'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00

You can read more about it here

Teletha
  • 603
  • 1
  • 11
  • 21