简体   繁体   中英

JS: Convert Datestamp into unix timestamp with converting to local time

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM