Fastest method of building a timestamp of the current hour
I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)
Currently I'm doing the following:
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var hour = d.getUTCHours();
d = new Date(year, month, day, hour);
console.log(d);
console.log(d.getTime());
Is it possible to avoid the second invocation of Date?
javascript datetime
add a comment |
I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)
Currently I'm doing the following:
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var hour = d.getUTCHours();
d = new Date(year, month, day, hour);
console.log(d);
console.log(d.getTime());
Is it possible to avoid the second invocation of Date?
javascript datetime
add a comment |
I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)
Currently I'm doing the following:
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var hour = d.getUTCHours();
d = new Date(year, month, day, hour);
console.log(d);
console.log(d.getTime());
Is it possible to avoid the second invocation of Date?
javascript datetime
I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)
Currently I'm doing the following:
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var hour = d.getUTCHours();
d = new Date(year, month, day, hour);
console.log(d);
console.log(d.getTime());
Is it possible to avoid the second invocation of Date?
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var hour = d.getUTCHours();
d = new Date(year, month, day, hour);
console.log(d);
console.log(d.getTime());
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var hour = d.getUTCHours();
d = new Date(year, month, day, hour);
console.log(d);
console.log(d.getTime());
javascript datetime
javascript datetime
asked Nov 14 '18 at 23:32
cdarwincdarwin
1,51272952
1,51272952
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:
var d = new Date();
d.setMinutes(0,0);
console.log(d);
console.log(d.getTime());
You could make it a oneliner, since setMinutes() already returns a timestamp:
var timestamp = new Date().setMinutes(0,0);
Is there a way to avoid calling date completely, if I start from a timestamp?
– cdarwin
Nov 14 '18 at 23:42
You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.
– Jeff
Nov 14 '18 at 23:44
add a comment |
Not sure why you're doing that twice, you really don't need to.
var d = new Date();
console.log(d.getHours());
// or
console.log(d.getTime() );
I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour
– cdarwin
Nov 14 '18 at 23:40
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53310337%2ffastest-method-of-building-a-timestamp-of-the-current-hour%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:
var d = new Date();
d.setMinutes(0,0);
console.log(d);
console.log(d.getTime());
You could make it a oneliner, since setMinutes() already returns a timestamp:
var timestamp = new Date().setMinutes(0,0);
Is there a way to avoid calling date completely, if I start from a timestamp?
– cdarwin
Nov 14 '18 at 23:42
You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.
– Jeff
Nov 14 '18 at 23:44
add a comment |
If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:
var d = new Date();
d.setMinutes(0,0);
console.log(d);
console.log(d.getTime());
You could make it a oneliner, since setMinutes() already returns a timestamp:
var timestamp = new Date().setMinutes(0,0);
Is there a way to avoid calling date completely, if I start from a timestamp?
– cdarwin
Nov 14 '18 at 23:42
You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.
– Jeff
Nov 14 '18 at 23:44
add a comment |
If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:
var d = new Date();
d.setMinutes(0,0);
console.log(d);
console.log(d.getTime());
You could make it a oneliner, since setMinutes() already returns a timestamp:
var timestamp = new Date().setMinutes(0,0);
If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:
var d = new Date();
d.setMinutes(0,0);
console.log(d);
console.log(d.getTime());
You could make it a oneliner, since setMinutes() already returns a timestamp:
var timestamp = new Date().setMinutes(0,0);
edited Nov 14 '18 at 23:47
answered Nov 14 '18 at 23:41
JeffJeff
6,34911025
6,34911025
Is there a way to avoid calling date completely, if I start from a timestamp?
– cdarwin
Nov 14 '18 at 23:42
You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.
– Jeff
Nov 14 '18 at 23:44
add a comment |
Is there a way to avoid calling date completely, if I start from a timestamp?
– cdarwin
Nov 14 '18 at 23:42
You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.
– Jeff
Nov 14 '18 at 23:44
Is there a way to avoid calling date completely, if I start from a timestamp?
– cdarwin
Nov 14 '18 at 23:42
Is there a way to avoid calling date completely, if I start from a timestamp?
– cdarwin
Nov 14 '18 at 23:42
You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.
– Jeff
Nov 14 '18 at 23:44
You would have to calculate the minutes+seconds from the given timestamp to subtract them. I don't know of an easier version.
– Jeff
Nov 14 '18 at 23:44
add a comment |
Not sure why you're doing that twice, you really don't need to.
var d = new Date();
console.log(d.getHours());
// or
console.log(d.getTime() );
I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour
– cdarwin
Nov 14 '18 at 23:40
add a comment |
Not sure why you're doing that twice, you really don't need to.
var d = new Date();
console.log(d.getHours());
// or
console.log(d.getTime() );
I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour
– cdarwin
Nov 14 '18 at 23:40
add a comment |
Not sure why you're doing that twice, you really don't need to.
var d = new Date();
console.log(d.getHours());
// or
console.log(d.getTime() );
Not sure why you're doing that twice, you really don't need to.
var d = new Date();
console.log(d.getHours());
// or
console.log(d.getTime() );
var d = new Date();
console.log(d.getHours());
// or
console.log(d.getTime() );
var d = new Date();
console.log(d.getHours());
// or
console.log(d.getTime() );
answered Nov 14 '18 at 23:36
SnowmonkeySnowmonkey
3,07211012
3,07211012
I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour
– cdarwin
Nov 14 '18 at 23:40
add a comment |
I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour
– cdarwin
Nov 14 '18 at 23:40
I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour
– cdarwin
Nov 14 '18 at 23:40
I need a new timestamp, your solution doesn't seem to answer, d.getTime is the current instant not the instant at the beginning of the hour
– cdarwin
Nov 14 '18 at 23:40
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.
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%2f53310337%2ffastest-method-of-building-a-timestamp-of-the-current-hour%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