Home > Software engineering >  JS: Convert Datestamp into unix timestamp with converting to local time
JS: Convert Datestamp into unix timestamp with converting to local time

Time:07-24

I have the following code to convert a datetime stamp into a unix timestamp:

const blah = Math.floor(new Date(dateString).getTime() / 1000);

if I pass in date string: "2022-07-23 17:12:22" I should expect 1658596342 however the conversion returns 1658592742 which is in my local time.

I am wondering how I can make the return in UTC without the conversion?

The assumption is the date string is always in UTC (but without the 'z' or 'gmt' or 'utc' identifier). So how can I make the return default as UTC?

Thanks

CodePudding user response:

add a capital T between date and time and add 0000 as suffix to force an utc date time stamp.

console.log('local time: '   (new Date("2022-07-23 17:12:22").getTime() / 1000))
console.log('  utc time: '   (new Date("2022-07-23T17:12:22 0000").getTime() / 1000))

  • Related