const d = new Date(); console.log(d); // output is 2022-04-11T10:29:46.644Z
The above code showing time for GMT, but i want to print time for Ex: GMT 1/GMT 05:30 in format like "2022-04-11T10:29:46.644 01:00: OR "2022-04-11T10:29:46.644 05:30".
I am using postman so can not use moment-timezone
CodePudding user response:
you can timezone info from new Date().getTimezoneOffset()
const timeoffset = new Date().getTimezoneOffset()
console.log(timeoffset)
//output : -330 which is GMT -330minutes
//you can convert to hours using
const inHours = timeoffset>0 ?
'-' Math.floor(timeoffset/60).toString().padStart(2,'0') ':' (timeoffset`).toString().padEnd(2,'0')
:
Math.floor((-1*timeoffset)/60).toString().padStart(2,'0') ':' ((-1*timeoffset)`).toString().padEnd(2,'0')
console.log(inHours);
CodePudding user response:
If moment.js is not an option, can you can use dayjs - https://day.js.org/