Created
October 14, 2016 21:36
-
-
Save dustinsmith1024/02383deb1e25d2e835d201262c8ea8cf to your computer and use it in GitHub Desktop.
Sample for testing moment parsing methods and timezones
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
"use strict"; | |
console.log(process.env.TZ); | |
// process.env.TZ = "Asia/Shanghai"; | |
let moment = require('moment'); | |
const format = "YYYY-MM-DD"; | |
let d, dUTC, dTZ, dJS; | |
const hours = ["00","01","02","03","04","05","06","07","08","09","10","11","12", | |
"13","14","15","16","17","18","19","20","21","22","23","24"]; | |
hours.forEach(hour => { | |
let toParse = "2016-09-03T"+hour+":00:00.000Z"; | |
d = moment(toParse); | |
dUTC = moment.utc(toParse); | |
dTZ = moment.parseZone(toParse); | |
dJS = new Date(toParse); | |
console.log(`-----Hour: ${hour} ----------`); | |
console.log(d.format(format)); | |
console.log(dUTC.format(format)); | |
console.log(dTZ.format(format)); | |
console.log(formatDate(dJS)); | |
console.log(dJS); | |
}); | |
function formatDate(date){ | |
let year = ""+date.getUTCFullYear(); | |
let month = date.getUTCMonth() + 1; | |
let day = date.getUTCDate(); | |
if (month < 10) { | |
month = "0" + month; | |
}else{ | |
month = "" + month; | |
} | |
if (day < 10) { | |
day = "0" + day; | |
}else{ | |
day = "" + day; | |
} | |
return year + "-" + month + "-" + day; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment