my javascript app must create events, which will be stored as UTC in the database, so that they can be shown afterwards in 3 different timezones.
the tricky part that i found difficult to figure out is that, when creating the event, the user must select a timezone along with the date.
The idea is: - user selects date + desired timezone from an additional dropdownlist with timezones. - i store as UTC in database - all users see the date in 3 different timezones.
You might ask why would anyone need that additional dropdownlist to select another timezone, when selecting the date with a datepicker ALREADY contains a timezone by default.
Example: Let be Jim, US citizen, who planned events all his life using EDT Washington time; he is visiting China, enters a chinese internet cafe, and wants to plan an event using this app. the datepicker will choose the local timezone which is China Standard Time. But Jim wants to plan by EDT and to be sure that the app handles everything properly.
Therefore he must specifically choose from an additional dropdownlist, the desired timezone.
So my question is. because i allow the user to select the desired timezone, do i first have to CONVERT the date the user entered, to THAT timezone, before converting it to UTC and only then afterwards store it? Or am I not interested in the timezone conversion at all, when saving the event in the database?
So which step is correct: - get local date + selected timezone - convert local date to user selected timezone - convert date to UTC - store to DB - when reading, convert in 3 timezones by using the selected timezone
or - get local date + selected timezone - convert date to UTC, disregard timezone - store to DB - when reading, convert in 3 timezones by using the selected timezone
later edit - i do this in meteor , so javascript server side. DB is mongodb and therefore for performance reasons the date must be saved as a JS date object (in utc ofcourse).
2nd edit
Below is the implementation i tried (it does not work, as in when I enter an event date of 07:00 AM KST, when outputing the end result read back from the database and converted back in that KST timezone shows anything but 07:00 AM)
it all starts here - this is a server side method which reads the date from a datepicker, the time from a timepicker, and the timezone from a dropdownlist:
var pStartDate = GetDateAndTimeFromPostData(eventAttributes.startDate, eventAttributes.startTime, eventAttributes.timezone);
here I attempt to build the selected date from the different controls (datepicker, timepicker, timezone ddl):
function GetDateAndTimeFromPostData(dt, tm, timezone)
{
var t = tm.split(":");
var hour = t[0];
var min = t[1];
var finalDate = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate(), hour, min);
var utcConverted = ConvertUserTimezoneToServerTimezone(finalDate, timezone);
return utcConverted;
}
here i attempt the timezone conversion:
function ConvertUserTimezoneToServerTimezone(dateToConvert, tz)
{
var userTimezonedDate;
switch(tz)
{
case "EDT":
{
userTimezonedDate = moment.tz(dateToConvert, "America/New_York");
break;
}
case "CEST":
{
userTimezonedDate = moment.tz(dateToConvert, "Europe/Berlin");
break;
}
case "KST":
{
userTimezonedDate = moment.tz(dateToConvert, "Asia/Seoul");
break;
}
case "CST":
{
userTimezonedDate = moment.tz(dateToConvert, "Asia/Shanghai");
break;
}
}
var utcDateFromUserTimezonedDate = userTimezonedDate.utc().toDate();
return utcDateFromUserTimezonedDate;
}
The fault is already in the code above, as in the utc date is not saved as KST, but as GMT (my local timezone).
As a side note, i dont quite understand how moment timezone converts; when I go on the moment timezone website and I write in chrome dev tools this:
var x = new Date();
var y = moment.tz(x, "Asia/Seoul");
y.utc().toDate()
i actually expect back a date object that shows KST right? but it shows GMT+2, my local tz.
Mon Sep 08 2014 23:44:05 GMT+0200 (Central Europe Daylight Time)
I also tried thinking this backwards, like how should the stored date object look like but thats also confusing - should it be saved as the selected timezone? e.g. Mon Sep 08 2014 23:44:05 GMT+0900 (KST) If not, how must my stored date object look like?
again, i use javascript server side with meteor and mongoDB as the database.
many thanks,