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

yearMonth.atEndOfMonth() / date.atStartOfDay() #729

Closed
thojanssens opened this issue Jul 7, 2020 · 5 comments
Closed

yearMonth.atEndOfMonth() / date.atStartOfDay() #729

thojanssens opened this issue Jul 7, 2020 · 5 comments
Labels
feedback v2? Candidate for being moved to a Temporal v2 proposal

Comments

@thojanssens
Copy link

thojanssens commented Jul 7, 2020

Hi 👋

Can we provide a function to convert a YearMonth to a Date at the end of the month? Or how do you see the user achieving this?

yearMonth.atEndOfMonth()

Similar to what Java and js-joda provide:
https://docs.oracle.com/javase/8/docs/api/java/time/YearMonth.html#atEndOfMonth--

This also reinforces the idea that to me, it's better to have atDate, atTime, atDay, atYear functions, or maybe go with withDate, withTime, withDay, withYear as there is already a with function:
#722 (comment)
This also avoids toDateOnDay, toDateInYear namings.

Or if we want to keep current namings, then it should be named toDateAtEndOfMonth. If using the with- approach, it would be named withDayAtEndOfMonth which I think is ok.

Similarly, be able to convert a date to a datetime at the start of the day.

date.toDateTime(Temporal.Time.from('00:00'))

can become this:

date.atStartOfDay();

What is more readable and declarative?:)

Or toDateTimeAtStartOfDay to keep current namings. If using the with- approach, it would be named withTimeAtStartOfDay.

@thojanssens thojanssens changed the title yearMonth.atEndOfMonth() yearMonth.atEndOfMonth() / date.atStartOfDay() Jul 7, 2020
@ptomato
Copy link
Collaborator

ptomato commented Jul 7, 2020

how do you see the user achieving this?

The current way would be yearMonth.toDate(yearMonth.daysInMonth) (add OnDay should #723 land).

I'll file this under "feedback", thanks! Can you say more about what the use case was where you bumped into the lack of this method?

@thojanssens
Copy link
Author

js-joda's atEndOfMonth was used for building a date picker component; I have to iterate through every day of the month, from 1 to the end of the month.

yearMonth.atEndOfMonth() becomes
yearMonth.toDate(yearMonth.daysInMonth)

atStartOfDay was used to build a sequence of intervals throughout the day. For example
[2020-07-07 00:00:00 - 2020-07-07 08:00:00,
2020-07-07 08:00:00 - 2020-07-07 19:30:00,
2020-07-07 19:30:00 - 2020-07-08 00:00:00]

date.atStartOfDay() becomes
date.toDateTime(Temporal.Time.from('00:00'))

The latter bothers me more than the lack of atEndOfMonth.

@justingrant
Copy link
Collaborator

FWIW, I've also been bothered by the non-DRY of Temporal.Time.from('00:00').

Should the Time parameter of Date.prototype.toDateTime() be optional, where an omitted parameter means "midnight"?

And/or should midnight be a frozen constant on Time, e.g. Temporal.Time.MIDNIGHT?

@justingrant
Copy link
Collaborator

justingrant commented Sep 10, 2020

FYI, #860 fixed part of this suggestion by making the time argument optional in Date.prototype.toDateTime() . The code below now works:

Temporal.now.date().toDateTime(); // time defaults to 00:00

We're also discussing how to turn a Date into a LocalDateTime, e.g.

Temporal.now.date().toLocalDateTime('America/Sao_Paulo'); // time defaults to start of day

An advantage if we offered this method is that the time would default to "start of day" which is usually midnight but could be another time, e.g. if DST started at midnight, or if the date uses a calendar where days don't start at midnight. It's also more ergonomic than having to go from Date=>DateTime=>LocalDateTime.

@ptomato ptomato added the v2? Candidate for being moved to a Temporal v2 proposal label Jan 14, 2021
@ptomato
Copy link
Collaborator

ptomato commented Feb 12, 2021

This isn't included in the revision of Temporal that's currently under review, but we can follow up for consideration in a future revision: js-temporal/proposal-temporal-v2#9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback v2? Candidate for being moved to a Temporal v2 proposal
Projects
None yet
Development

No branches or pull requests

3 participants