Skip to content

Instantly share code, notes, and snippets.

@stormwild
Last active August 9, 2022 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stormwild/bb6790d9363f10105e286f259499ad4b to your computer and use it in GitHub Desktop.
Save stormwild/bb6790d9363f10105e286f259499ad4b to your computer and use it in GitHub Desktop.
Notes on DateTimeOffset

Date Conversions

MS SQL Server

Create Replicable Setup To verify

If the database server timezone is running on UTC and timestamps are generated on the database as the default value using any date functions these dates would be the utc datetime at the time the row was inserted or updated.

If API Server system timezone is also utc then generated datetimeoffsets would be utc datetimes

Conversion on the client

Research on best practices

Convert UTC date time to local date time

In my point of view servers should always in the general case return a datetime in the standardized ISO 8601-format.

More info here:

IN this case the server would return '2011-06-29T16:52:48.000Z' which would feed directly into the JS Date object.

var utcDate = '2011-06-29T16:52:48.000Z';  // ISO-8601 formatted date returned from server
var localDate = new Date(utcDate);

The localDate will be in the right local time which in my case would be two hours later (DK time).

You really don't have to do all this parsing which just complicates stuff, as long as you are consistent with what format to expect from the server.

Client side conversion on local date to utc

How to convert a Date to UTC?

var isoDateString = new Date().toISOString();
console.log(isoDateString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment