-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to transform seconds to time #641
Comments
Is this coming? The |
the |
@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 EDIT: nvm, this only works for durations that are less than 1 day. If you call Looks like 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 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')
} |
Here is my React solution, using i18next pluralization to format the parts: const { t } = useTranslation()
const start = dayjs(startDate)
const end = dayjs(endDate)
const days = end.diff(start, 'days')
const totalHours = end.diff(start, 'hours')
const totalMinutes = end.diff(start, 'minutes')
const totalSeconds = end.diff(start, 'seconds')
const hours = totalHours - days * 24
const minutes = totalMinutes - totalHours * 60
const seconds = totalSeconds - totalMinutes * 60
const parts: string[] = []
if (days > 0) {
parts.push(t('dateTimeParts.days', { count: days }))
}
if (hours > 0) {
parts.push(t('dateTimeParts.hours', { count: hours }))
}
if (minutes > 0) {
parts.push(t('dateTimeParts.minutes', { count: minutes }))
}
if (seconds > 0) {
parts.push(t('dateTimeParts.seconds', { count: seconds }))
}
console.log(parts.join(', ')) |
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:
Thanks
The text was updated successfully, but these errors were encountered: