0

I'm working on a web app which uses a calendar. When I create a calendar event and select a start time of 10:00am, the event is saved as 09:00am (I'm based in UK and we are UTC+1 currently which I think accounts for the difference.

I'd like to know the best way to handle this, and make sure if a user from any time zone chooses 10:00am on the calendar, that 10:00am is written to the database.

Here are the relevant bits of code. I'm specifically interested in the Start and Finish variables which are both Datetime datatypes throughout:

This is the Javascript to catch the event creation:

eventDrop: function (calEvent, dayDelta, minuteDelta, allDay, revertFunc) {
                var event = {};
                event.EventID = calEvent.eventid;
                event.StatusString = "Update";
                event.CustomerID = calEvent.customerid;
                event.ProductID = calEvent.productid;
                event.Title = calEvent.title;
                event.Start = new Date(calEvent.start.format()); 
                event.Finish = new Date(calEvent.end.format());
                event.EventNote = calEvent.eventNote;
                event.ProductValue = calEvent.ProductValue;

That's passed to an SQL handler

                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = m_Connection;
                cmd.CommandText = "usp_ATP_Calendar_InsertUpdateEvent";
                [...other params...]
                cmd.Parameters.AddWithValue("@Start", eve.Start);
                cmd.Parameters.AddWithValue("@Finish", eve.Finish);
                cmd.Parameters.AddWithValue("@EventNote", eve.EventNote);

                cmd.ExecuteNonQuery();

I've added an alert to the web page which is just event.Start.toString() whihc is triggered when an event is saved. It shows this for this example:

Click for alert image

So I assume that 10:00+0100 equates to 9:00am which is why my event is getting written away with a 'different' time. Can anyone advise the best way to deal with this, and perhaps provide some example code so that if a user drops an event on 10:00am that is the time that is stored?

  • Dont use DateTime, use DateTimeOffset - that includes timezone info. – Avo Nappo May 15 '18 at 15:19
  • Are those moment.js objects where you call `.format()`? Can you give more details about where they come from, and why you create `Date` objects? Also, how does this eventually get to the server side? There's a lot of steps not shown here... – Matt Johnson-Pint May 15 '18 at 16:24
  • @AvoNappo That's not true. Neither `DateTime` nor `DateTimeOffset` includes timezone information. As an extra, `DateTimeOffset` includes [UTC offset](https://en.wikipedia.org/wiki/UTC_offset) value but that _doesn't mean_ that it includes timezone information. It is **not** possible to know the timezone information only based a UTC offset value. – Soner Gönül May 16 '18 at 05:23

1 Answers1

1

Until you don't want to perform some date calculations in your application the most safe way is to store all the values in UTC and then convert them to a client's time zone — this way your code won't depend on server actually executing it.

It means you shouldn't really save 10:00am to your store, you first translate it to UTC accounting client's time zone. Then convert it back to client's time zone, when you want to show it.

This approach is not generally applicable, so consider to check links 1, 2 and 3 for more descriptive info.

Uladzislaŭ
  • 1,680
  • 10
  • 13
  • This advice is fine for timestamping, such as logging things as they happen, but is not the best way to handle scheduling. This is covered in the best practices article you linked to. – Matt Johnson-Pint May 15 '18 at 16:20