Home > database >  Convert unix time to local iso time format to set in htmk date time picker
Convert unix time to local iso time format to set in htmk date time picker

Time:11-22

So, I have a unix time as endingTime= 1669060881 and then I do this to convert it into ISO format this.edate = new Date(endingTime*1000).toISOString() ===> 2022-11-21T20:01:21.000Z

slicing it so that it can fir html local date input this.edate = this.edate.slice(0, -8) ===> 2022-11-21T20:01

and then patch this value in my date time picker of form this.userForm['endingTime']['controls'].patchValue(this.edate);

html ---> shows result as 21-11-22 and 8:01 as the time

when I put the time 1669060881 in here https://www.epochconverter.com/ you can see two times

GMT: Monday, November 21, 2022 8:01:21 PM Your time zone: Tuesday, November 22, 2022 1:31:21 AM GMT 05:30

I need the iso conversiton in "Your time zone" time but currentt edate val is the GMT timezone

pls help

CodePudding user response:

You can use toLocaleString instead of toISOString

Date.toLocaleString

const ms = 1669060881 * 1000
const dateTime = new Date(ms).toLocaleString()
console.log(dateTime)

CodePudding user response:

Don't re-invent the wheel and use some popular and well tested library when working with dates. Like zonedTimeToUtc from date-fns-tz.

 const dateTime = zonedTimeToUtc(ms, 'Your timezone')
  • Related