How to change Local datetime into UTC Format

DateTime has a Kind property, which can have one of the three values:

    Unspecified
    Local
    Utc


DateTime convertedDate = DateTime.SpecifyKind(
    DateTime.Parse(dateStr),
    DateTimeKind.Utc);

var kind = convertedDate.Kind; // will equal DateTimeKind.Utc
Now, once the system knows its in UTC time, you can just call ToLocalTime:

DateTime dt = convertedDate.ToLocalTime();
This will give you the result you require.

No comments:

Post a Comment