Questions tagged [datetimeoffset]

DateTimeOffset is a data structure that encapsulates a DateTime value and the Offset from UTC that the value reflects. It is used to represent an instantaneous moment in time, while still capturing the perspective of the observer.

DateTimeOffset is a data structure that encapsulates a DateTime value and the Offset from UTC that the value reflects. It is used to represent an instantaneous moment in time, while still capturing the perspective of the observer.

Contrary to popular belief (and to much of the documentation) the offset provided does not represent a time zone. One might start with a time zone to obtain the offset, but it is a one-way function. You can not determine the time zone by the offset alone. For a better understanding of this, see this community wiki answer.

The preferred serialization format for this type of data is defined as part of the ISO-8601 specification.

  • Example: 1997-07-16T19:20:30+01:00

DateTimeOffset and similar structures are presented in:

Please update this list if you are aware of similar structures in other languages, frameworks, or databases

527 questions
2101
votes
30 answers

Daylight saving time and time zone best practices

I am hoping to make this question and the answers to it the definitive guide to dealing with daylight saving time, in particular for dealing with the actual change overs. If you have anything to add, please do Many systems are dependent on keeping…
Oded
  • 463,167
  • 92
  • 837
  • 979
857
votes
10 answers

DateTime vs DateTimeOffset

Currently, we have a standard way of dealing with .NET DateTime's in a TimeZone aware way: Whenever we produce a DateTime we do it in UTC (e.g. using DateTime.UtcNow), and whenever we display one, we convert back from UTC to the user's local time.…
David Reis
  • 10,471
  • 5
  • 31
  • 42
100
votes
3 answers

Storing DateTime (UTC) vs. storing DateTimeOffset

I usually have an "interceptor" that right before reading/writing from/to the database does DateTime conversion (from UTC to local time, and from local time to UTC), so I can use DateTime.Now (derivations and comparisions) throughout the system…
Frederico
  • 1,001
  • 2
  • 8
  • 3
88
votes
10 answers

Subtract 1 day with PHP

I'm trying to take a date object that's coming out of my Drupal CMS, subtract one day and print out both dates. Here's what I have $date_raw = $messagenode->field_message_date[0]['value']; print($date_raw); //this gives me the following string:…
td-dev
  • 883
  • 1
  • 6
  • 6
86
votes
4 answers

How to use TimeZoneInfo to get local time during Daylight Saving Time?

I'm trying to use DateTimeOffset to convey a specific moment in time across any time zone. I can't figure out how to use TimeZoneInfo to deal with daylight saving time. var dt = DateTime.UtcNow; Console.WriteLine(dt.ToLocalTime()); var tz =…
jaminto
  • 3,645
  • 3
  • 28
  • 35
71
votes
4 answers

pandas out of bounds nanosecond timestamp after offset rollforward plus adding a month offset

I am confused how pandas blew out of bounds for datetime objects with these lines: import pandas as pd BOMoffset = pd.tseries.offsets.MonthBegin() # here some code sets the all_treatments dataframe and the newrowix, micolix, mocolix…
László
  • 3,132
  • 6
  • 29
  • 45
66
votes
5 answers

Determine Whether Daylight Savings Time (DST) is Active in Java for a Specified Date

I have a Java class that takes in the latitude/longitude of a location and returns the GMT offset when daylight savings time is on and off. I am looking for an easy way to determine in Java if the current date is in daylight savings time so I can…
gelgamil
  • 841
  • 1
  • 7
  • 6
58
votes
6 answers

How can I convert a Sql Server 2008 DateTimeOffset to a DateTime

I'm hoping to convert a table which has a DATETIMEOFFSET field, down to a DATETIME field BUT recalculates the time by taking notice of the offset. This, in effect, converts the value to UTC. eg. CreatedOn: 2008-12-19 17:30:09.0000000 +11:00 that…
Pure.Krome
  • 78,923
  • 102
  • 356
  • 586
58
votes
2 answers

Best practices with saving datetime & timezone info in database when data is dependant on datetime

There were quite a few questions about saving datetime & timezones info in DB but more on the overall level. Here I'd like to address a specific case. System specs We have an Orders system database It is a multi-tenant system where tenants can use…
daneejela
  • 8,695
  • 6
  • 29
  • 40
54
votes
3 answers

When Would You Prefer DateTime Over DateTimeOffset

A few months ago I was introduced to the new DateTimeOffset type and was glad DateTime's flaws with regard to time zones were finally taken care of. However, I was left wondering if there were any overhead or problems that could occur from using…
Omer van Kloeten
  • 11,272
  • 9
  • 38
  • 53
46
votes
8 answers

Why does Json.NET DeserializeObject change the timezone to local time?

I'm using json.net to deserialize a DateTimeOffset, but it is ignoring the specified timezone and converting the datetime to the local offset. For example, given var content = @"{""startDateTime"":""2012-07-19T14:30:00+09:30""}"; When deserialised…
Dave Capper
  • 463
  • 1
  • 4
  • 6
41
votes
2 answers

Difference between 'DateTime' and 'DateTimeOffset'

What is difference between a DateTime and a DateTimeOffset object? And when should we use each one? In a web-application that may change the server's area, storing date and time. Which one is better, or is there any other suggestions?
agent47
  • 6,467
  • 15
  • 50
  • 79
35
votes
2 answers

Convert DateTimeOffset to DateTime and add offset to this DateTime

I have DateTimeOffset: DateTimeOffset myDTO = DateTimeOffset.ParseExact( "2015/01/15 17:37:00 -0500", "yyyy/MM/dd HH:mm:ss zzz", CultureInfo.InvariantCulture); Console.WriteLine(myDTO); Result =>…
Alex
  • 6,714
  • 26
  • 84
  • 140
29
votes
3 answers

Convert UTC DateTime to DateTimeOffset

I need to convert UTC date strings to DateTimeOffsets. This must work with a timezone which differs from the computers timezone. E.g. current computer timezone is +02:00, but I want to create a DateTimeOffset with offset -4:00. I already read lot of…
Fabian
  • 731
  • 2
  • 8
  • 16
27
votes
3 answers

Does Java 8's new Java Date Time API take care of DST?

I am thinking of using the new java 8 Date Time API. I googled a bit and found jodaTime as good choice for java but still kind of interested to see how this new API works. I am storing all time in UTC values in my datastore and will be converting…
Raja
  • 587
  • 1
  • 7
  • 20
1
2 3
35 36