Skip to content
Advertisement

Dayjs difference between two date time has a default of 8hours

I am confused why the difference between these two dates has a default of 8 hours? I am expecting a result something like 00:12:32 at least in my case from the time I posted this. Right now, in the result, the hour has a default of 8.

const date1 = dayjs(Date.now())
const date2 = dayjs(1674235010388)

// Check date date1
console.log('date1 =', dayjs(date1).format('hh:mm:ss'))

// Check date2 date
console.log('date2 =', dayjs(date2).format('hh:mm:ss'))

const result = date1.diff(date2)
console.log(dayjs(result).format('hh:mm:ss'))
<script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>

Advertisement

Answer

I cannot tell you but it is another reason for not using these frameworks when plain JS is more predictible

const ms2hhmmss = ms => new Date(ms).toISOString().slice(11,19); // grab the time from YYYY-MM-DDTHH:MM:SS.milZ

const date1 = new Date()
const date2 = new Date(1674235010388)
console.log(date1)
console.log(date2)

const diff = Math.abs(date1 - date2)
console.log(ms2hhmmss(diff))
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement