0

I am building an event calendar app, and I am a bit unsure about how this will output for different timezones.

  1. User create an event, select start and end date + time
  2. The event is displayed in a calendar on selected date and time.

This is how I format the datetime for display in calendar:

Data how it is stored / retrieved from DB:

var startDate = '2014-03-09 12:00:00.000';
var endDate = '2014-03-09 17:00:00.000';

Format of date before output to calendar (basicly how far I have gotten on this):

var start = new Date(startDate.replace(' ', 'T'));
var end = new Date(endDate.replace(' ', 'T'));

console.log('start: ' + start);
console.log('end: ' + end);

Console Log:

start: Sun Mar 09 2014 12:00:00 GMT+0100 (UTC)
end: Sun Mar 09 2014 17:00:00 GMT+0100 (UTC)

This approach is working just fine, for my timezone, however, I am not sure how this will work for another timezone, example in USA?

The event will start march 9, 12:00 , and if my approach for another timezone will "change" the start/end time based on user timezone, for the calendars event list, this will not be good.

So my question is, will my approach work across timezones, or will it need more work to accomplish what I am looking for? Any and all suggestions are appreciated :)

Tom
  • 3,717
  • 5
  • 26
  • 28

2 Answers2

0

Your date string '2014-03-09 12:00:00.000' diesnt have any timezone info with it, so its upto the discretion of browser implementation to choose one I guess, and taken as UTC

instead if you have dates stored in format like

"2011-12-19T15:28:46.493Z" it specifies the time is in UTC

So if anyone does a new Date("2011-12-19T15:28:46.493Z") they will get the local date time corresponding to this UTC date. Provided, the browser supports Date constructor with ISODateString check @ckozl answer here

so something like

new Date('2014-03-09 12:00:00.000'.replace(' ','T') + 'Z')

should work fine, just add custom ISOParser for browsers that doesnt support ISO constructor if that matters you.

Community
  • 1
  • 1
sabithpocker
  • 15,274
  • 1
  • 42
  • 75
0

It's just a logical question.

As the time of events is input by users, all the jobs your server or the database needs is to store it.

Assume that I want to create an event which will start at 2013-03-09 17:00:00.000 GMT+0800, I'll just enter or pick, maybe, 03-09 17:00 then submit this string to your server.

If it's stored as string in your database, everything works fine even you do nothing about the timezone. Because your server does simply transport job without transforming. I still get the same string shown on my browser.

In another case, you store the time string as a type of datetime. Before you put it into database, you have to convert it. Remember that you also need a reverse conversion when retrieve it and response to the client. During these two conversions if no parameter of timezone is taken (just yyyy-MM-dd HH:mm:ss), eventually I can get exact the same as I have input before.

Sorry for my poor english. I'm not sure if I make it clear.

Jiang
  • 590
  • 2
  • 10