none
DayOfWeek Question RRS feed

  • General discussion

  • When I use DayOfWeek, a property of System.DateTime instance. I got two different value from the same date on two different server.

    Why this happened?

    My date is 2013-2-16

    Code is:  

       DateTime dt = DateTime.Parse('2013-2-16');

       Console.WrilteLine(dt.DayOfWeek);

    The expected result is:  Saturday.

    On server 1, the result is: Sunday.

    On server 2, the result is: Saturday.

    Sunday, February 17, 2013 3:19 AM

All replies

  • The problem is with the usage og DateTime.Parse method which uses the current thread culture for converting date time which may vary betweeen system regional settings.. Better you can use overloaded method of Parse to specify the format or TryParseExact.

    DateTime dt = DateTime.Parse('2013-2-16',CultureInfo.CreateSpecificCulture("en-US"));
    Console.WrilteLine(dt.DayOfWeek);

    Alternatively you can use

    DateTime dt = new DateTime(2013,2, 16);
    Console.WrilteLine(dt.DayOfWeek);


    It all Happenz Sendil


    • Edited by sendilg Sunday, February 17, 2013 5:08 AM
    Sunday, February 17, 2013 5:03 AM
  • Thank you sendilg.

    But I think it's not this reason. Because from this afternoon, it was corrected without changing any code.

    Now I can not reappear it. Sucks.

    Sunday, February 17, 2013 8:41 AM
  • Are you sure it was (exactly)  this code, because for instance the used culture setting of the Server can cause what you write for instance for a date like 1-2-2013. On USA English Servers this one returns Wednesday, on English English servers it returns Friday.

    Be aware that the culture invariant is created for this English behavior so I thought that 10-16-2013 is always seen as non USA.


    Success
    Cor


    Sunday, February 17, 2013 11:34 AM
  • Yes.

    One server is English Server at Singapore and another is Chinese Server at BeiJing.

    Monday, February 18, 2013 6:36 AM
  • I am sure that this date formate "2013-2-16" can be complied and runned correctly.

    Monday, February 18, 2013 6:40 AM