0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.