Custom TimeZone Conversion for Single Page Apps
moment-browser.js is an extension to moment.js that helps convert Dates to/from the Browser Timezone to the TimeZone required by your browser based applications. E.g. You have a datepicker which the user expects to be in New York TZ, but you are using the application in the Pacific Timezones. When you display your server provided UTC Date Times they will show in the Browser TZ. This moment extension will create a new Date which helps offset the difference between the browser TZ and the TZ required by the application.
Usage
Works with browser environments.
<!-- Browser -->
Methods
moment().browserConverter()
Creates a converter helper object which handles conversion to and from the Browser TZ.
var newYorkDate = momentBrowser.tz("2017-02-02 12:00", "America/New_York");
var converter = newYorkDate.browserConverter();
converter.asDisplayTz(displayTimeZone, <browserTimeZone>);
Creates a new date object which is a datetime modified to replicate the display timezone. browserTimeZone is optional the code will try and guess it.
var newYorkDateInBrowser = converter.asDisplayTz('America/New_York');
var newYorkDateInBrowser = converter.asDisplayTz('America/New_York', 'America/Los_Angeles');
converter.fromDisplayTz(date);
Now a user makes a selection in the UI and changes the DateTime. On the workflow back to the server we need to convert this date back to it's correct current browser TimeZone value.
var onePmPST = momentBrowser.tz("2017-02-02 13:00", "America/Los_Angeles");
var newYorkDateOnePm = converter.fromDisplayTz(onePmPST)