How can I setLocaleHours() on a date object?
up vote
3
down vote
favorite
When I execute
myDate = new Date('2000-02-02 12:30:00')
I get a date object like this 2000-02-02T11:30:00.000Z because there is a difference of one hour between my timezone (Europe/Vienna) and UTC.
I can now change the hour by doing
myDate.setHours(10)
and the result will be a date object like this 2000-02-02T09:30:00.000Z because of the one hour difference.
I can also set the UTC hours by
myDate.setUTCHours(10)
to get a dateobject like this 2000-02-02T10:30:00.000Z
I'm looking for something similar to
myDate.setLocaleHours(10, "America/New_York")
(which doesn't exist)
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
javascript node.js date timezone
add a comment |
up vote
3
down vote
favorite
When I execute
myDate = new Date('2000-02-02 12:30:00')
I get a date object like this 2000-02-02T11:30:00.000Z because there is a difference of one hour between my timezone (Europe/Vienna) and UTC.
I can now change the hour by doing
myDate.setHours(10)
and the result will be a date object like this 2000-02-02T09:30:00.000Z because of the one hour difference.
I can also set the UTC hours by
myDate.setUTCHours(10)
to get a dateobject like this 2000-02-02T10:30:00.000Z
I'm looking for something similar to
myDate.setLocaleHours(10, "America/New_York")
(which doesn't exist)
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
javascript node.js date timezone
moment.js timezone plugin would be a help here
– charlietfl
Nov 8 at 11:36
Thanks, I have heard of it but it's my last resort because I would like to have a solution without dependencies.
– Zauz
Nov 8 at 11:39
Then perhaps you are better off looking for an API source that can give you the current offset for a specific timezone to use insetHours. Issues like daylight savings time make it complex
– charlietfl
Nov 8 at 11:44
Possible duplicate of Create a Date with a set timezone without using a string representation
– ponury-kostek
Nov 8 at 12:27
"2000-02-02 12:30:00" is not a format supported by ECMA-262 so paring is implementation dependent. It results in an invalid Date in at least one current browser. You might consider the timeZone option of toLocaleString.
– RobG
Nov 8 at 20:21
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
When I execute
myDate = new Date('2000-02-02 12:30:00')
I get a date object like this 2000-02-02T11:30:00.000Z because there is a difference of one hour between my timezone (Europe/Vienna) and UTC.
I can now change the hour by doing
myDate.setHours(10)
and the result will be a date object like this 2000-02-02T09:30:00.000Z because of the one hour difference.
I can also set the UTC hours by
myDate.setUTCHours(10)
to get a dateobject like this 2000-02-02T10:30:00.000Z
I'm looking for something similar to
myDate.setLocaleHours(10, "America/New_York")
(which doesn't exist)
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
javascript node.js date timezone
When I execute
myDate = new Date('2000-02-02 12:30:00')
I get a date object like this 2000-02-02T11:30:00.000Z because there is a difference of one hour between my timezone (Europe/Vienna) and UTC.
I can now change the hour by doing
myDate.setHours(10)
and the result will be a date object like this 2000-02-02T09:30:00.000Z because of the one hour difference.
I can also set the UTC hours by
myDate.setUTCHours(10)
to get a dateobject like this 2000-02-02T10:30:00.000Z
I'm looking for something similar to
myDate.setLocaleHours(10, "America/New_York")
(which doesn't exist)
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
javascript node.js date timezone
javascript node.js date timezone
asked Nov 8 at 11:28
Zauz
259210
259210
moment.js timezone plugin would be a help here
– charlietfl
Nov 8 at 11:36
Thanks, I have heard of it but it's my last resort because I would like to have a solution without dependencies.
– Zauz
Nov 8 at 11:39
Then perhaps you are better off looking for an API source that can give you the current offset for a specific timezone to use insetHours. Issues like daylight savings time make it complex
– charlietfl
Nov 8 at 11:44
Possible duplicate of Create a Date with a set timezone without using a string representation
– ponury-kostek
Nov 8 at 12:27
"2000-02-02 12:30:00" is not a format supported by ECMA-262 so paring is implementation dependent. It results in an invalid Date in at least one current browser. You might consider the timeZone option of toLocaleString.
– RobG
Nov 8 at 20:21
add a comment |
moment.js timezone plugin would be a help here
– charlietfl
Nov 8 at 11:36
Thanks, I have heard of it but it's my last resort because I would like to have a solution without dependencies.
– Zauz
Nov 8 at 11:39
Then perhaps you are better off looking for an API source that can give you the current offset for a specific timezone to use insetHours. Issues like daylight savings time make it complex
– charlietfl
Nov 8 at 11:44
Possible duplicate of Create a Date with a set timezone without using a string representation
– ponury-kostek
Nov 8 at 12:27
"2000-02-02 12:30:00" is not a format supported by ECMA-262 so paring is implementation dependent. It results in an invalid Date in at least one current browser. You might consider the timeZone option of toLocaleString.
– RobG
Nov 8 at 20:21
moment.js timezone plugin would be a help here
– charlietfl
Nov 8 at 11:36
moment.js timezone plugin would be a help here
– charlietfl
Nov 8 at 11:36
Thanks, I have heard of it but it's my last resort because I would like to have a solution without dependencies.
– Zauz
Nov 8 at 11:39
Thanks, I have heard of it but it's my last resort because I would like to have a solution without dependencies.
– Zauz
Nov 8 at 11:39
Then perhaps you are better off looking for an API source that can give you the current offset for a specific timezone to use in
setHours. Issues like daylight savings time make it complex– charlietfl
Nov 8 at 11:44
Then perhaps you are better off looking for an API source that can give you the current offset for a specific timezone to use in
setHours. Issues like daylight savings time make it complex– charlietfl
Nov 8 at 11:44
Possible duplicate of Create a Date with a set timezone without using a string representation
– ponury-kostek
Nov 8 at 12:27
Possible duplicate of Create a Date with a set timezone without using a string representation
– ponury-kostek
Nov 8 at 12:27
"2000-02-02 12:30:00" is not a format supported by ECMA-262 so paring is implementation dependent. It results in an invalid Date in at least one current browser. You might consider the timeZone option of toLocaleString.
– RobG
Nov 8 at 20:21
"2000-02-02 12:30:00" is not a format supported by ECMA-262 so paring is implementation dependent. It results in an invalid Date in at least one current browser. You might consider the timeZone option of toLocaleString.
– RobG
Nov 8 at 20:21
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
How can I setLocaleHours() on a date object?
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
You can't. At least, not on the Date object. It has no ability to set time based on an arbitrary time zone.
There is work in progress to rectify this, by adding a new set of standard objects to ECMAScript. See the TC39 Temporal proposal for more details. The temporal ZonedInstant will have functionality to work with named time zones.
However, for now, you will need a library that understands time zones. Moment-timezone is one option, though, these days the Moment team recommends Luxon for modern app development. Another great option is js-Joda.
It's slightly awkward in Luxon because the setters always use the DateTime's zone, so you need something likedt.setZone("America/New_York").set({ hours: 10 }).setZone("local")
– snickersnack
Nov 11 at 16:31
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
How can I setLocaleHours() on a date object?
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
You can't. At least, not on the Date object. It has no ability to set time based on an arbitrary time zone.
There is work in progress to rectify this, by adding a new set of standard objects to ECMAScript. See the TC39 Temporal proposal for more details. The temporal ZonedInstant will have functionality to work with named time zones.
However, for now, you will need a library that understands time zones. Moment-timezone is one option, though, these days the Moment team recommends Luxon for modern app development. Another great option is js-Joda.
It's slightly awkward in Luxon because the setters always use the DateTime's zone, so you need something likedt.setZone("America/New_York").set({ hours: 10 }).setZone("local")
– snickersnack
Nov 11 at 16:31
add a comment |
up vote
1
down vote
accepted
How can I setLocaleHours() on a date object?
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
You can't. At least, not on the Date object. It has no ability to set time based on an arbitrary time zone.
There is work in progress to rectify this, by adding a new set of standard objects to ECMAScript. See the TC39 Temporal proposal for more details. The temporal ZonedInstant will have functionality to work with named time zones.
However, for now, you will need a library that understands time zones. Moment-timezone is one option, though, these days the Moment team recommends Luxon for modern app development. Another great option is js-Joda.
It's slightly awkward in Luxon because the setters always use the DateTime's zone, so you need something likedt.setZone("America/New_York").set({ hours: 10 }).setZone("local")
– snickersnack
Nov 11 at 16:31
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
How can I setLocaleHours() on a date object?
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
You can't. At least, not on the Date object. It has no ability to set time based on an arbitrary time zone.
There is work in progress to rectify this, by adding a new set of standard objects to ECMAScript. See the TC39 Temporal proposal for more details. The temporal ZonedInstant will have functionality to work with named time zones.
However, for now, you will need a library that understands time zones. Moment-timezone is one option, though, these days the Moment team recommends Luxon for modern app development. Another great option is js-Joda.
How can I setLocaleHours() on a date object?
What is the best way to set the hours to a specific value in a timezone which is not my current one and also not UTC?
You can't. At least, not on the Date object. It has no ability to set time based on an arbitrary time zone.
There is work in progress to rectify this, by adding a new set of standard objects to ECMAScript. See the TC39 Temporal proposal for more details. The temporal ZonedInstant will have functionality to work with named time zones.
However, for now, you will need a library that understands time zones. Moment-timezone is one option, though, these days the Moment team recommends Luxon for modern app development. Another great option is js-Joda.
answered Nov 9 at 18:26
Matt Johnson
134k40271394
134k40271394
It's slightly awkward in Luxon because the setters always use the DateTime's zone, so you need something likedt.setZone("America/New_York").set({ hours: 10 }).setZone("local")
– snickersnack
Nov 11 at 16:31
add a comment |
It's slightly awkward in Luxon because the setters always use the DateTime's zone, so you need something likedt.setZone("America/New_York").set({ hours: 10 }).setZone("local")
– snickersnack
Nov 11 at 16:31
It's slightly awkward in Luxon because the setters always use the DateTime's zone, so you need something like
dt.setZone("America/New_York").set({ hours: 10 }).setZone("local")– snickersnack
Nov 11 at 16:31
It's slightly awkward in Luxon because the setters always use the DateTime's zone, so you need something like
dt.setZone("America/New_York").set({ hours: 10 }).setZone("local")– snickersnack
Nov 11 at 16:31
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206832%2fhow-can-i-setlocalehours-on-a-date-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
moment.js timezone plugin would be a help here
– charlietfl
Nov 8 at 11:36
Thanks, I have heard of it but it's my last resort because I would like to have a solution without dependencies.
– Zauz
Nov 8 at 11:39
Then perhaps you are better off looking for an API source that can give you the current offset for a specific timezone to use in
setHours. Issues like daylight savings time make it complex– charlietfl
Nov 8 at 11:44
Possible duplicate of Create a Date with a set timezone without using a string representation
– ponury-kostek
Nov 8 at 12:27
"2000-02-02 12:30:00" is not a format supported by ECMA-262 so paring is implementation dependent. It results in an invalid Date in at least one current browser. You might consider the timeZone option of toLocaleString.
– RobG
Nov 8 at 20:21