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

Create a new date from a year, a month, a day, an hour, a minute, a second and a millisecond. #214

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
2 participants
@guyonvarch-zen

guyonvarch-zen commented Apr 12, 2015

In a project for my company, I need to get the time corresponding to the begin of the day from a given time. Using Date.fromTime and the string representation to get the begin of the day fail because of locals, and passing from the string representation is a poor choice to get what I want I think.

Firefox and chrome behave differently with the following expression. Firefox take the local time while chrome take the UTC time, look at the output on my machine:

new Date("2015-04-10T00:00:00.000")
Firefox: Date 2015-04-09T22:00:00.000Z
Chrome: Fri Apr 10 2015 02:00:00 GMT+0200 (CEST)

I can't rely on Date.fromTime for my problem.

However, I found a solution with the following function :

function beginOfDay(date) {
var msPerDay = 24 * 60 * 60 * 1000;
var offsetMs = new Date().getTimezoneOffset() * 60 * 1000;

var ms = date.valueOf() - offsetMs;
var beginMs = ms - (ms % msPerDay) + offsetMs;

return new Date(beginMs);

}

But it have to rely on getTimezoneOffset(), it is simpler with a newDate function that works with locals:

new Date(2015, 04, 10, 0, 0, 0, 0)
Firefox: Date 2015-05-09T22:00:00.000Z
Chrome: Sun May 10 2015 22:00:00 GMT+0200 (CEST)

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jan 19, 2016

Contributor

Is still still relevant/needed? If so, maybe you can use functionality from http://package.elm-lang.org/packages/rluiten/elm-date-extra/latest/? Generally, the way towards adding new functionality to core is via such ...-extra packages. So even if your needs are not currently addressed by the linked package, the best strategy would be to target it with a pull request (instead of core).

Contributor

jvoigtlaender commented Jan 19, 2016

Is still still relevant/needed? If so, maybe you can use functionality from http://package.elm-lang.org/packages/rluiten/elm-date-extra/latest/? Generally, the way towards adding new functionality to core is via such ...-extra packages. So even if your needs are not currently addressed by the linked package, the best strategy would be to target it with a pull request (instead of core).

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jan 19, 2016

Contributor

... except if you absolutely need this to be done on the native/JavaScript side. In that case, getting it realized in a non-core package would be (more) problematic.

Contributor

jvoigtlaender commented Jan 19, 2016

... except if you absolutely need this to be done on the native/JavaScript side. In that case, getting it realized in a non-core package would be (more) problematic.

@guyonvarch-zen

This comment has been minimized.

Show comment
Hide comment
@guyonvarch-zen

guyonvarch-zen Jan 25, 2016

What I need is to get the date corresponding to the begin or the end of the day.

This can be done with new Date(…) and number parameters, or with the setHours method, or finally with new Date().getTimezoneOffset() like I mentioned in the first post.

The only core function giving us the date is in the Date module: fromString : String -> Result String Date.

I found a way to get the same result with new Date(str: String), but I still have to give the time zone, which I have not ?

Same result in chrome and firefox:

new Date("Fri May 10 2015 00:00:00 GMT+0200 (CEST)").getTime()
1431208800000

new Date(2015, 04, 10, 0, 0, 0, 0).getTime()
1431208800000

guyonvarch-zen commented Jan 25, 2016

What I need is to get the date corresponding to the begin or the end of the day.

This can be done with new Date(…) and number parameters, or with the setHours method, or finally with new Date().getTimezoneOffset() like I mentioned in the first post.

The only core function giving us the date is in the Date module: fromString : String -> Result String Date.

I found a way to get the same result with new Date(str: String), but I still have to give the time zone, which I have not ?

Same result in chrome and firefox:

new Date("Fri May 10 2015 00:00:00 GMT+0200 (CEST)").getTime()
1431208800000

new Date(2015, 04, 10, 0, 0, 0, 0).getTime()
1431208800000
@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jan 25, 2016

Contributor

I don't get from your response whether or not what you want can be done with http://package.elm-lang.org/packages/rluiten/elm-date-extra/latest/ or as a pull request against that package. You describe what you would do in JavaScript, but the package I mention does already provide a wrapper around at least some of JavaScript's date API. I suggest you do look at that package and see how far it, or its approach, can get you. Without clear indication that that package can't possibly give you what you want, there is no strong case to make for adding functionality to core.

For context on this "policy", see https://github.com/elm-lang/core/blob/master/CONTRIBUTING.md#adding-new-functions.

Contributor

jvoigtlaender commented Jan 25, 2016

I don't get from your response whether or not what you want can be done with http://package.elm-lang.org/packages/rluiten/elm-date-extra/latest/ or as a pull request against that package. You describe what you would do in JavaScript, but the package I mention does already provide a wrapper around at least some of JavaScript's date API. I suggest you do look at that package and see how far it, or its approach, can get you. Without clear indication that that package can't possibly give you what you want, there is no strong case to make for adding functionality to core.

For context on this "policy", see https://github.com/elm-lang/core/blob/master/CONTRIBUTING.md#adding-new-functions.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Feb 4, 2016

Contributor

@guyonvarch, any response to what I said in my last comment above?

Contributor

jvoigtlaender commented Feb 4, 2016

@guyonvarch, any response to what I said in my last comment above?

@guyonvarch-zen

This comment has been minimized.

Show comment
Hide comment
@guyonvarch-zen

guyonvarch-zen Feb 8, 2016

The function of this PR was recently added in elm-date-extra :
rluiten/elm-date-extra@2e2c489#diff-b171d4b828e1da35cd5803d9eb0ec412R178

I tested it on a day on firefox and chrome and got the same result as with new Date(…). We can close this PR.

guyonvarch-zen commented Feb 8, 2016

The function of this PR was recently added in elm-date-extra :
rluiten/elm-date-extra@2e2c489#diff-b171d4b828e1da35cd5803d9eb0ec412R178

I tested it on a day on firefox and chrome and got the same result as with new Date(…). We can close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment