locked
DateTime.Ticks and ToFileTime are inconsistent - why? RRS feed

  • Question

  • The following returns the same Ticks values for the two DateTime instances, but different ToFileTime values. The ToFileTime values differ by the offset between my local timezone and UTC (as expected). But why are the Ticks values the same for the two timezones?

     

     

    var dt1 = new DateTime(2010, 9, 21, 0, 0, 0, DateTimeKind.Utc);

     

     

    var ticks1 = dt1.Ticks;

     

     

    var ft1 = dt1.ToFileTime();

     

     

    var dt1a = new DateTime(2010, 9, 21, 0, 0, 0, DateTimeKind.Local);

     

     

    var ticks1a = dt1a.Ticks;

     

     

    var ft1a = dt1a.ToFileTime();

    Friday, January 21, 2011 11:53 PM

Answers

  • The Ticks property ignores the DateTimeKind setting.  ToFileTime() takes this into account.  That's why you see the shift in values - its the difference between your Local time and Utc.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Reed Copsey, JrMVP Monday, January 24, 2011 3:41 PM
    • Marked as answer by dthouston Wednesday, January 26, 2011 5:51 PM
    Saturday, January 22, 2011 2:09 AM

All replies

  • The Ticks property ignores the DateTimeKind setting.  ToFileTime() takes this into account.  That's why you see the shift in values - its the difference between your Local time and Utc.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Reed Copsey, JrMVP Monday, January 24, 2011 3:41 PM
    • Marked as answer by dthouston Wednesday, January 26, 2011 5:51 PM
    Saturday, January 22, 2011 2:09 AM
  • Hi dthouston,

    Thank you for posting.

    Any update? Has your question been resolved?

    Please remember to click “Mark as Answer” on the post that helps you, this can be beneficial to the other community members reading the thread.

    Best Regards,

    Larcolais


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 24, 2011 9:26 AM