Created
January 30, 2018 12:39
-
-
Save itsdouges/0dc9a1babc936d7d0eeb67c979264ce3 to your computer and use it in GitHub Desktop.
Parse and serialize moment timezones correctly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import moment from 'moment-timezone'; | |
// Override moments default behaviour so we can save timezone data. | |
moment.fn.toJSON = function() { | |
const timeZoneId = this.tz(); | |
return timeZoneId ? `${this.format()}|${timeZoneId}` : this.format(); | |
}; | |
const dateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.{0,1}\d*))(?:Z|(\+|-)([\d|:]*))?(|.*)?$/; | |
export function parse(json: string) { | |
const data = JSON.parse(json, (_, value) => { | |
if (typeof value === 'string' && dateRegex.exec(value)) { | |
const [date, timeZoneId] = value.split('|'); | |
return timeZoneId ? moment.tz(date, timeZoneId) : moment(date); | |
} | |
return value; | |
}); | |
return data; | |
} | |
// tslint:disable-next-line ban-types | |
export function stringify(object: Object) { | |
const data = JSON.stringify(object); | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment