Home > Enterprise >  How to format a date string in ISO 8601 format(2022-04-10T15:58:25.549 05:30) without moment.js in J
How to format a date string in ISO 8601 format(2022-04-10T15:58:25.549 05:30) without moment.js in J

Time:04-11

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/

  • Related