DateTime.Now and Culture/Timezone specific

It sounds like you need to store a DateTimeOffset instead of a DateTime. You could just store the local DateTime to the user creating the value, but that means you can’t perform any ordering operations etc. You can’t just use DateTime.UtcNow, as that won’t store anything to indicate the local date/time of the user when the record was created.

Alternatively, you could store an instant in time along with the user’s time zone – that’s harder to achieve, but would give you more information as then you’d be able to say things like “What is the user’s local time one hour later?”

The hosting of the server should be irrelevant – you should never use the server’s time zone. However, you will need to know the appropriate UTC offset (or time zone) for the user. This cannot be done based on just the culture – you’ll want to use Javascript on the user’s machine to determine the UTC offset at the time you’re interested in (not necessarily “now”).

Once you’ve worked out how to store the value, retrieving it is simple – if you’ve already stored the UTC instant and an offset, you just apply that offset and you’ll get back to the original user’s local time. You haven’t said how you’re converting values to text, but it should just drop out simply – just format the value, and you should get the original local time.

If you decide to use Noda Time, you’d just use OffsetDateTime instead of DateTimeOffset.

Leave a Comment