none
DateTimeOffset to DateTime Conversion and ViceVersa - Data Loss RRS feed

  • Question

  • When I convert a datetimeoffset value to a datetime value, is there any possibility for data loss. From MSDN documentation, the conversion from datetimeoffset to datetime is mentioned as follows:

    The DateTime property is most commonly used to perform DateTimeOffset to DateTime conversion. However, it returns a DateTime value whose Kind property is Unspecified. This means that any information about the DateTimeOffset value's relationship to UTC is lost by the conversion when the DateTime property is used.

    To indicate that a converted DateTime value is the UTC time, you can retrieve the value of the DateTimeOffset.UtcDateTime property. It differs from the DateTime property in two ways:

    It returns a DateTime value whose Kind property is Utc. If the Offset property value does not equal TimeSpan.Zero, it converts the time to UTC.

    I see the following method to convert datetime offset to datetime:

    static DateTime ConvertFromDateTimeOffset(DateTimeOffset dateTime)
    {
       if (dateTime.Offset.Equals(TimeSpan.Zero))
          return dateTime.UtcDateTime;
       else if (dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime)))
          return DateTime.SpecifyKind(dateTime.DateTime, DateTimeKind.Local);
       else
          return dateTime.DateTime;   
    }
    Now in our system we are converting datetimeoffset to datetime in the above way. Later we want to convert the datetime back to datetimeoffset.

    As an example:

    DateTime dt = ConvertFromDateTimeOffset(datetimeOffset);
    DateTimeOffset dofsetnew = new DateTimeOffset(dt);
    My question is whether under any circumstances datetimeOffset and dofsetnew be different ? If so then conversion would be loss data.
    Tuesday, June 5, 2012 11:08 PM

Answers

All replies

  • No, it is the same.

    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Wednesday, June 6, 2012 10:07 AM
  • Update?

    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.

    Thursday, June 7, 2012 8:15 AM