Skip to content
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

Closed
Wyzix33 opened this issue Jul 24, 2019 · 5 comments
Closed

Ability to transform seconds to time #641

Wyzix33 opened this issue Jul 24, 2019 · 5 comments

Comments

@Wyzix33
Copy link

Wyzix33 commented Jul 24, 2019

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

@iamkun
Copy link
Owner

iamkun commented Jul 29, 2019

#564

@iamkun iamkun closed this as completed Jul 29, 2019
@brett-east
Copy link

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.

@iamkun
Copy link
Owner

iamkun commented Apr 27, 2020

the duration plugin does not have a format method. Nor does moment#duration

@guanzo
Copy link

guanzo commented Jun 15, 2020

@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')
}

@peterpoe
Copy link

peterpoe commented Jul 2, 2024

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(', '))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants