-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
time.toSecondOfDay() #730
Comments
Filed under "feedback", thanks again! Here the same question, can you say anything more about the use case where you missed this method? |
Welcome, @thojanssens! Thanks so much for taking the time to review Temporal and give feedback. It's very helpful. ECMAScript built-in objects tend to have fewer methods than OSS libraries built on top of those built-ins. The general philosophy is to expose few methods that can flexibly be applied to many different use cases. Therefore it's unlikely that Temporal would build a dedicated method for a use case like "how many seconds have elapsed so far today?". However, the more general use case of "[unit] of [larger unit]" is an interesting one. There are already For the specific case of "second of day", here's one way to do it. function toSecondOfDay(timeLike) {
const time = Temporal.Time.from(timeLike);
const midnight = Temporal.Time.from('00:00');
const diff = time.difference(midnight, {largestUnit: 'seconds'});
return diff.seconds;
}
toSecondOfDay({hour: 12})
// => 43200
toSecondOfDay(Temporal.DateTime.from('2020-07-07T10:34:56'))
// => 38096
toSecondOfDay(Temporal.Time.from('12:00'))
// => 43200 (although currently 0. See note below.) BTW, while writing this sample I found bug #735 in |
@ptomato For example, get the difference between two times in total of seconds. AFAIK @justingrant or function toSecondOfDay(timeLike) {
const time = Temporal.Time.from(timeLike);
return time.hour * 60 * 60 + time.minute * 60 + time.second;
} |
To get the difference between two times in total of seconds, you can do |
Getting totals from an existing duration is discussed here: #584 |
This use case has gotten a little easier from recent changes. Here's what this code will look like in a few days after #1064 lands: |
I believe these use cases are covered well enough now. // toSecondOfDay as Justin mentioned above:
plainTime.since('00:00').total({ unit: 'seconds' })
// get the difference between two times in seconds:
time1.until(time2).total({ unit: 'seconds' }) I'm not sure there's anything to propose at https://github.com/js-temporal/proposal-temporal-v2/issues at this point, but please do if you think there is. |
Can we provide a function to convert a Time to the total number of seconds of the day?
Similar to what Java and js-joda provide:
https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html#toSecondOfDay--
The text was updated successfully, but these errors were encountered: