dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Converting timestamp to utc returns same timestamp

Open Arctomachine opened this issue 1 year ago • 0 comments

Probably not bug, but lack of information in documentation on this topic.

I want to convert timestamp of known timezone into utc timestamp. I follow timezone section in docs https://day.js.org/docs/en/timezone/converting-to-zone But as result I get exactly same timestamp I provided as input.

const dayjs = require('dayjs')
dayjs.extend(require('dayjs/plugin/utc'))
dayjs.extend(require('dayjs/plugin/timezone'))

const timestamp = 1709864000000
const timezone = 'Europe/Moscow' // it is +3 timezone

const expectedUtc = timestamp - 3 * 60 * 60 * 1000 // so should be minus 3 hours
const utc = dayjs(timestamp).tz(timezone).utc().valueOf()

console.log('Input timestamp:', timestamp)
console.log('Expected result:', expectedUtc)
console.log('Actual result:', utc)
console.log('Expected == actual?', expectedUtc == utc)
console.log('Input == actual?', timestamp == utc)
Input timestamp: 1709864000000
Expected result: 1709853200000
Actual result: 1709864000000
Expected == actual? false     
Input == actual? true

What am I doing wrong here? And how to do right?

Arctomachine avatar Mar 08 '24 02:03 Arctomachine