$(function(){
//local time zone is Asia/Ho_Chi_Minh
//today is Oct 20th, and user A create a booking starting at 10 AM Nov 4th
//we just use local date toString method and send this string to server
let localTimeStringSentToServer = new Date('2021-11-04 10:00:00').toString();
//we can see that localTimeStringSentToServer has the local time part, and a time zone offset part
$("#localTime").text("Local time converted to string: " +localTimeStringSentToServer);
//today is Oct 20th, and user A create a booking starting at 10 AM Nov 4th
//we convert this local time to a UTC string (RFC 1123) and send this string to server
let utcTimeStringSentToServer = new Date('2021-11-04 10:00:00').toUTCString();
$("#utcTime").text("Local time converted to UTC string: " + utcTimeStringSentToServer);
//assume server send back the localTimeStringSentToServer and we translate to Stockholm timezone
//this result is WRONG
$("#momentTzTime1").text("Translated to Stockholm time from saved localTimeString: " + moment.tz(localTimeStringSentToServer, 'Europe/Stockholm').format('LLL'));
//assume server send back the utcTimeStringSentToServer and we translate to Stockholm timezone
//this result is CORRECT
$("#momentTzTime2").text("Translated to Stockholm time from saved utcTimeString: " + moment.tz(utcTimeStringSentToServer, 'Europe/Stockholm').format('LLL'));
//As you can see UTC time string is a better option to use.
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.