How can I get this timestamp format with javascript / jquery?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I format a Microsoft JSON date?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How to check whether a checkbox is checked in jQuery?How do I include a JavaScript file in another JavaScript file?Where can I find documentation on formatting a date in JavaScript?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
Can we decompose every group element to elements of order 2? (using Cayley's theorem to identificate the group with permutations)
Someone who is granted access to information but not expected to read it
What is the theme of analysis?
How do I properly use a function under a class?
Parsing text written the millitext font
Print "N NE E SE S SW W NW"
Simple log rotation script
What does this line mean in Zelazny's The Courts of Chaos?
David slept with Bathsheba because she was pure?? What does that mean?
Realistic, logical way for men with medieval-era weaponry to compete with much larger and physically stronger foes
Is it true that "only photographers care about noise"?
Can an open source licence be revoked if it violates employer's IP?
Is the first of the 10 Commandments considered a mitzvah?
A life of PhD: is it feasible?
What is Gilligan's full name?
Must I use my personal social media account for work?
Must a CPU have a GPU if the motherboard provides a display port (when there isn't any separate video card)?
How can religions without a hell discourage evil-doing?
Jam with honey & without pectin has a saucy consistency always
Is fission/fusion to iron the most efficient way to convert mass to energy?
Why did the AvroCar fail to fly above 3 feet?
Can I use 220 V outlets on a 15 ampere breaker and wire it up as 110 V?
Are athlete's college degrees discounted by employers and graduate school admissions?
usage of mir gefallen
How can I get this timestamp format with javascript / jquery?
How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I format a Microsoft JSON date?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How to check whether a checkbox is checked in jQuery?How do I include a JavaScript file in another JavaScript file?Where can I find documentation on formatting a date in JavaScript?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I want to get this format :
2019-03-24 15:05:20
Here is what I have tried:
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
I got:
2019-3-24 15:0:20
It's missing leading zeroes.
javascript jquery
add a comment |
I want to get this format :
2019-03-24 15:05:20
Here is what I have tried:
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
I got:
2019-3-24 15:0:20
It's missing leading zeroes.
javascript jquery
add a comment |
I want to get this format :
2019-03-24 15:05:20
Here is what I have tried:
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
I got:
2019-3-24 15:0:20
It's missing leading zeroes.
javascript jquery
I want to get this format :
2019-03-24 15:05:20
Here is what I have tried:
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
I got:
2019-3-24 15:0:20
It's missing leading zeroes.
javascript jquery
javascript jquery
asked Mar 25 at 0:02
JosueJosue
61
61
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
That's because numbers lower than 10 should be padded with zero but they aren't.
You can use padStart
function to achieve the expected result - add a leading 0 character if the length of a number is less than 2. I modified your code a little bit but all you need is to use .toString().padStart(2, '0')
on a number.
var today = new Date();
var date = [
today.getFullYear(),
today.getMonth() + 1,
today.getDate(),
].map((value) => value.toString().padStart(2, '0')).join('-');
var time = [
today.getHours(),
today.getMinutes(),
today.getSeconds(),
].map((value) => value.toString().padStart(2, '0')).join(':');;
var dateTime = date + ' ' + time;
console.log(dateTime);
add a comment |
This is pretty close to ISO 8601, so let's start with that.
const d = new Date();
d
.toISOString() // Convert date to a string in the format of 2019-03-25T00:07:22.0253Z
.substr(0, 19) // Strip off the milliseconds and Zulu timezone indication
.replace('T', ' '); // Replace the T for "time" with a space
This leaves you with a date formatted like 2019-03-25 00:07:22
.
Why was this downvoted? It's a perfectly reasonable solution to the problem presented, and is well documented. I really don't understand Stack Overflow these days.
– Brad
Mar 25 at 1:22
add a comment |
You can use ternary operation for simple use. This should give you leading zero.
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + (today.getMinutes() < 10 ? '0' : '') + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;
console.log(dateTime, 'result');
add a comment |
You can use:
var d = new Date();
d = new Date(d.getTime() - 3000000);
var date_format_str = d.getFullYear().toString()+"-"+((d.getMonth()+1).toString().length==2?(d.getMonth()+1).toString():"0"+(d.getMonth()+1).toString())+"-"+(d.getDate().toString().length==2?d.getDate().toString():"0"+d.getDate().toString())+" "+(d.getHours().toString().length==2?d.getHours().toString():"0"+d.getHours().toString())+":"+((parseInt(d.getMinutes()/5)*5).toString().length==2?(parseInt(d.getMinutes()/5)*5).toString():"0"+(parseInt(d.getMinutes()/5)*5).toString())+":00";
console.log(date_format_str);
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%2f55329754%2fhow-can-i-get-this-timestamp-format-with-javascript-jquery%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
That's because numbers lower than 10 should be padded with zero but they aren't.
You can use padStart
function to achieve the expected result - add a leading 0 character if the length of a number is less than 2. I modified your code a little bit but all you need is to use .toString().padStart(2, '0')
on a number.
var today = new Date();
var date = [
today.getFullYear(),
today.getMonth() + 1,
today.getDate(),
].map((value) => value.toString().padStart(2, '0')).join('-');
var time = [
today.getHours(),
today.getMinutes(),
today.getSeconds(),
].map((value) => value.toString().padStart(2, '0')).join(':');;
var dateTime = date + ' ' + time;
console.log(dateTime);
add a comment |
That's because numbers lower than 10 should be padded with zero but they aren't.
You can use padStart
function to achieve the expected result - add a leading 0 character if the length of a number is less than 2. I modified your code a little bit but all you need is to use .toString().padStart(2, '0')
on a number.
var today = new Date();
var date = [
today.getFullYear(),
today.getMonth() + 1,
today.getDate(),
].map((value) => value.toString().padStart(2, '0')).join('-');
var time = [
today.getHours(),
today.getMinutes(),
today.getSeconds(),
].map((value) => value.toString().padStart(2, '0')).join(':');;
var dateTime = date + ' ' + time;
console.log(dateTime);
add a comment |
That's because numbers lower than 10 should be padded with zero but they aren't.
You can use padStart
function to achieve the expected result - add a leading 0 character if the length of a number is less than 2. I modified your code a little bit but all you need is to use .toString().padStart(2, '0')
on a number.
var today = new Date();
var date = [
today.getFullYear(),
today.getMonth() + 1,
today.getDate(),
].map((value) => value.toString().padStart(2, '0')).join('-');
var time = [
today.getHours(),
today.getMinutes(),
today.getSeconds(),
].map((value) => value.toString().padStart(2, '0')).join(':');;
var dateTime = date + ' ' + time;
console.log(dateTime);
That's because numbers lower than 10 should be padded with zero but they aren't.
You can use padStart
function to achieve the expected result - add a leading 0 character if the length of a number is less than 2. I modified your code a little bit but all you need is to use .toString().padStart(2, '0')
on a number.
var today = new Date();
var date = [
today.getFullYear(),
today.getMonth() + 1,
today.getDate(),
].map((value) => value.toString().padStart(2, '0')).join('-');
var time = [
today.getHours(),
today.getMinutes(),
today.getSeconds(),
].map((value) => value.toString().padStart(2, '0')).join(':');;
var dateTime = date + ' ' + time;
console.log(dateTime);
var today = new Date();
var date = [
today.getFullYear(),
today.getMonth() + 1,
today.getDate(),
].map((value) => value.toString().padStart(2, '0')).join('-');
var time = [
today.getHours(),
today.getMinutes(),
today.getSeconds(),
].map((value) => value.toString().padStart(2, '0')).join(':');;
var dateTime = date + ' ' + time;
console.log(dateTime);
var today = new Date();
var date = [
today.getFullYear(),
today.getMonth() + 1,
today.getDate(),
].map((value) => value.toString().padStart(2, '0')).join('-');
var time = [
today.getHours(),
today.getMinutes(),
today.getSeconds(),
].map((value) => value.toString().padStart(2, '0')).join(':');;
var dateTime = date + ' ' + time;
console.log(dateTime);
edited Mar 25 at 0:14
Ele
26.3k52354
26.3k52354
answered Mar 25 at 0:10
Tomasz KajtochTomasz Kajtoch
562513
562513
add a comment |
add a comment |
This is pretty close to ISO 8601, so let's start with that.
const d = new Date();
d
.toISOString() // Convert date to a string in the format of 2019-03-25T00:07:22.0253Z
.substr(0, 19) // Strip off the milliseconds and Zulu timezone indication
.replace('T', ' '); // Replace the T for "time" with a space
This leaves you with a date formatted like 2019-03-25 00:07:22
.
Why was this downvoted? It's a perfectly reasonable solution to the problem presented, and is well documented. I really don't understand Stack Overflow these days.
– Brad
Mar 25 at 1:22
add a comment |
This is pretty close to ISO 8601, so let's start with that.
const d = new Date();
d
.toISOString() // Convert date to a string in the format of 2019-03-25T00:07:22.0253Z
.substr(0, 19) // Strip off the milliseconds and Zulu timezone indication
.replace('T', ' '); // Replace the T for "time" with a space
This leaves you with a date formatted like 2019-03-25 00:07:22
.
Why was this downvoted? It's a perfectly reasonable solution to the problem presented, and is well documented. I really don't understand Stack Overflow these days.
– Brad
Mar 25 at 1:22
add a comment |
This is pretty close to ISO 8601, so let's start with that.
const d = new Date();
d
.toISOString() // Convert date to a string in the format of 2019-03-25T00:07:22.0253Z
.substr(0, 19) // Strip off the milliseconds and Zulu timezone indication
.replace('T', ' '); // Replace the T for "time" with a space
This leaves you with a date formatted like 2019-03-25 00:07:22
.
This is pretty close to ISO 8601, so let's start with that.
const d = new Date();
d
.toISOString() // Convert date to a string in the format of 2019-03-25T00:07:22.0253Z
.substr(0, 19) // Strip off the milliseconds and Zulu timezone indication
.replace('T', ' '); // Replace the T for "time" with a space
This leaves you with a date formatted like 2019-03-25 00:07:22
.
answered Mar 25 at 0:07
BradBrad
119k29243405
119k29243405
Why was this downvoted? It's a perfectly reasonable solution to the problem presented, and is well documented. I really don't understand Stack Overflow these days.
– Brad
Mar 25 at 1:22
add a comment |
Why was this downvoted? It's a perfectly reasonable solution to the problem presented, and is well documented. I really don't understand Stack Overflow these days.
– Brad
Mar 25 at 1:22
Why was this downvoted? It's a perfectly reasonable solution to the problem presented, and is well documented. I really don't understand Stack Overflow these days.
– Brad
Mar 25 at 1:22
Why was this downvoted? It's a perfectly reasonable solution to the problem presented, and is well documented. I really don't understand Stack Overflow these days.
– Brad
Mar 25 at 1:22
add a comment |
You can use ternary operation for simple use. This should give you leading zero.
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + (today.getMinutes() < 10 ? '0' : '') + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;
console.log(dateTime, 'result');
add a comment |
You can use ternary operation for simple use. This should give you leading zero.
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + (today.getMinutes() < 10 ? '0' : '') + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;
console.log(dateTime, 'result');
add a comment |
You can use ternary operation for simple use. This should give you leading zero.
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + (today.getMinutes() < 10 ? '0' : '') + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;
console.log(dateTime, 'result');
You can use ternary operation for simple use. This should give you leading zero.
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + (today.getMinutes() < 10 ? '0' : '') + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;
console.log(dateTime, 'result');
answered Mar 25 at 0:10
Sundar BanSundar Ban
464414
464414
add a comment |
add a comment |
You can use:
var d = new Date();
d = new Date(d.getTime() - 3000000);
var date_format_str = d.getFullYear().toString()+"-"+((d.getMonth()+1).toString().length==2?(d.getMonth()+1).toString():"0"+(d.getMonth()+1).toString())+"-"+(d.getDate().toString().length==2?d.getDate().toString():"0"+d.getDate().toString())+" "+(d.getHours().toString().length==2?d.getHours().toString():"0"+d.getHours().toString())+":"+((parseInt(d.getMinutes()/5)*5).toString().length==2?(parseInt(d.getMinutes()/5)*5).toString():"0"+(parseInt(d.getMinutes()/5)*5).toString())+":00";
console.log(date_format_str);
add a comment |
You can use:
var d = new Date();
d = new Date(d.getTime() - 3000000);
var date_format_str = d.getFullYear().toString()+"-"+((d.getMonth()+1).toString().length==2?(d.getMonth()+1).toString():"0"+(d.getMonth()+1).toString())+"-"+(d.getDate().toString().length==2?d.getDate().toString():"0"+d.getDate().toString())+" "+(d.getHours().toString().length==2?d.getHours().toString():"0"+d.getHours().toString())+":"+((parseInt(d.getMinutes()/5)*5).toString().length==2?(parseInt(d.getMinutes()/5)*5).toString():"0"+(parseInt(d.getMinutes()/5)*5).toString())+":00";
console.log(date_format_str);
add a comment |
You can use:
var d = new Date();
d = new Date(d.getTime() - 3000000);
var date_format_str = d.getFullYear().toString()+"-"+((d.getMonth()+1).toString().length==2?(d.getMonth()+1).toString():"0"+(d.getMonth()+1).toString())+"-"+(d.getDate().toString().length==2?d.getDate().toString():"0"+d.getDate().toString())+" "+(d.getHours().toString().length==2?d.getHours().toString():"0"+d.getHours().toString())+":"+((parseInt(d.getMinutes()/5)*5).toString().length==2?(parseInt(d.getMinutes()/5)*5).toString():"0"+(parseInt(d.getMinutes()/5)*5).toString())+":00";
console.log(date_format_str);
You can use:
var d = new Date();
d = new Date(d.getTime() - 3000000);
var date_format_str = d.getFullYear().toString()+"-"+((d.getMonth()+1).toString().length==2?(d.getMonth()+1).toString():"0"+(d.getMonth()+1).toString())+"-"+(d.getDate().toString().length==2?d.getDate().toString():"0"+d.getDate().toString())+" "+(d.getHours().toString().length==2?d.getHours().toString():"0"+d.getHours().toString())+":"+((parseInt(d.getMinutes()/5)*5).toString().length==2?(parseInt(d.getMinutes()/5)*5).toString():"0"+(parseInt(d.getMinutes()/5)*5).toString())+":00";
console.log(date_format_str);
answered Mar 25 at 0:17
Syrup72Syrup72
2117
2117
add a comment |
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%2f55329754%2fhow-can-i-get-this-timestamp-format-with-javascript-jquery%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