user584018
user584018

Reputation: 11354

dateTime stored in database in UTC, but when reading as C# objects it's automatically converted to Local Time, I want UTC only, how?

In database I am storing the datetime in UTC,

enter image description here

I have class property like below,

public DateTime CollectionTimeUtc { get; set; }

Now when I am reading from database into C# object, it's automatically converted back to local time,

enter image description here

How to avoid this conversion ? I want it UTC. Thanks!

Upvotes: 0

Views: 108

Answers (1)

kvp2982
kvp2982

Reputation: 151

Write your own explicit getter and setter, like the old way of writing properties in C#.

private DateTime collectionTimeUtc;

public DateTime CollectionTimeUtc
{
    get { return collectionTimeUtc; }
    set { collectionTimeUtc = value.ToUniversalTime(); }
}

Upvotes: 1

Related Questions