Skip to content

Instantly share code, notes, and snippets.

@Alexisvt
Last active July 15, 2020 16:13
Show Gist options
  • Save Alexisvt/c857922ceba46e9a2b5335296b70315d to your computer and use it in GitHub Desktop.
Save Alexisvt/c857922ceba46e9a2b5335296b70315d to your computer and use it in GitHub Desktop.
Code snippet showing how to transform a regular date to UTC and vice versa.
const moment = require('moment');
const regularDate = moment() // ?
const regularDateString = regularDate.format('YYYY-mm-DD HH:ss'); // ?
const utcDate = moment.utc() // ?
const utcString = utcDate.format('YYYY-mm-DD HH:ss') //?
// transforming regular to UTC
console.log(moment.utc(regularDate).format('YYYY-mm-DD HH:ss')) // ?
console.log(moment(regularDateString, 'YYYY-mm-DD HH:ss').utc().format('YYYY-mm-DD HH:ss')) // ?
// UTC to regular
moment.utc(utcDate).local().format('YYYY-mm-DD HH:ss'); // ?
moment.utc(utcString, 'YYYY-mm-DD HH:ss').local().format('YYYY-mm-DD HH:ss'); // ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment