Hi,
is it possible to transform seconds to time: for example 3602 seconds to 01:00:02, like a utility or something :)
just like moment has:
var seconds = 3820;
var duration = moment.duration(seconds, 'seconds');
var formatted = duration.format("hh:mm:ss");
console.log(formatted);
Thanks
Is this coming? The duration plugin has been released, but it doesn't have a format method, and I can't see anything in the documentation that explains how to format a duration as time.
the duration plugin does not have a format method. Nor does moment#duration
@brett-east unless i'm missing something, dayjs can already format a duration as time. Try plugging this into runkit: https://npm.runkit.com/dayjs
var dayjs = require("dayjs")
var duration = require('dayjs/plugin/duration')
var utc = require('dayjs/plugin/utc')
dayjs.extend(duration)
dayjs.extend(utc)
const oneMinuteAgo = dayjs().subtract(1, 'minute')
const dur = dayjs.duration(dayjs().diff(oneMinuteAgo))
dayjs.utc(dur.asMilliseconds()).format('HH:mm:ss') // "00:01:00"
For w/e reason, I have to use dayjs.utc() instead of dayjs() when formatting, otherwise the "hours" segment is off by 8 hours. Prolly some timezone crap.
EDIT: nvm, this only works for durations that are less than 1 day. If you call .format(), you'll see its actually a real date "1970-01-01T23:00:30Z", and the time segment just happens to work if duration < 1 day. Which explains the 8 hour offset "bug" i was seeing.
Looks like moment-duration-format is the moment way to format durations.. dayjs will need something similar
Plug this into runkit
var moment = require("moment")
var momentDurationFormat = require("moment-duration-format")
momentDurationFormat(moment)
const futureDate = moment().add(2, 'day')
const dur = moment.duration(futureDate.diff(moment()))
console.log(moment.utc(dur.asMilliseconds()).format('HH:mm:ss')) // "23:59:59" WRONG
console.log(dur.format('HH:mm:ss')) // "48:00:00" CORRECT
This is my hacky solution for when duration > 24 hours. Basically show X days instead of a formatted HH:mm:ss
const t = dayjs.duration(dateUserCanPost.diff(dayjs()))
if (t.asDays() > 1) {
return t.days() + ' day' + (t.days() > 1 ? 's' : '')
} else {
return dayjs.utc(t.asMilliseconds()).format('HH:mm:ss')
}
Most helpful comment
@brett-east unless i'm missing something, dayjs can already format a duration as time. Try plugging this into runkit: https://npm.runkit.com/dayjs
For w/e reason, I have to use
dayjs.utc()instead ofdayjs()when formatting, otherwise the "hours" segment is off by 8 hours. Prolly some timezone crap.EDIT: nvm, this only works for durations that are less than 1 day. If you call
.format(), you'll see its actually a real date"1970-01-01T23:00:30Z", and the time segment just happens to work if duration < 1 day. Which explains the 8 hour offset "bug" i was seeing.Looks like
moment-duration-formatis the moment way to format durations.. dayjs will need something similarPlug this into runkit
This is my hacky solution for when duration > 24 hours. Basically show
X daysinstead of a formattedHH:mm:ss