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?










share|improve this question






















  • 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

















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?










share|improve this question






















  • 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















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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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




















  • 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


















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














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.






share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer





















  • 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















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.






share|improve this answer





















  • 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













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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Landwehr

Reims

Schenkenzell