R
R
Ruslan2019-10-07 08:03:06
Working with date/time
Ruslan, 2019-10-07 08:03:06

How to store date and time considering timezone in sql server + C# and display via javascript?

Let's assume that people from different time zones use the site (they have different time zones set on their computers). The server is also in a certain time zone. We need to store some event date for the table rows, for example, the date of creation of the row in the database, and display it on the site via javascript, taking into account its (user's) time zone.
Those. If we have a line created at 9:38 Moscow time, then in Yekaterinburg 11:38 should be displayed.
How to organize it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Ruslan, 2019-10-07
@Razbezhkin

You can organize it like this:
1. For a table column, use the “ datetimeoffset ” type
2. To assign via C#, use the DateTimeOffset data type and its Now property
3. To transfer to the client (for example, via ajax), we use standard serialization (for example, via Newtonsoft. Json), you get a string like "2019-10-07T09:38:10.5016691+05:00", which indicates the local time (server) and its time zone.
4. On the client, we use this string to create a date object, and display it in the user's local time, for example, like this: (new Date(datestring)).toLocaleString()

P
Peter, 2019-10-07
@petermzg

1. Store the date on the server in UTC.
2. Send to clients in Unix Epoch
3. Convert "var date = new Date(unixtimestamp * 1000);" to Javascript

O
OnYourLips, 2019-10-07
@OnYourLips

Store the date on the server in UTC
Transmit to clients in UTC in string representation
In Javascript, convert to the client's TOR.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question