in Education by
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 questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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); Run code snippetExpand snippet

Related questions

0 votes
    I want to get this format : 2019-03-24 15:05:20 Here is what I have tried: var today = ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I want to get this format : 2019-03-24 15:05:20 Here is what I have tried: var today = ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    There are loads of questions about the current date in JavaScript in general, but in my scenario, I have ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I want to make a new span class called "button". When the "button" is clicked, it should run ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    I'm wondering how to stop an animation made with jQuery timers. I think I have tried everything, I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I am trying to return an javascript value with Awesome lib in Vb.net / C#. Now I know how to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    ALL I am confused about the date field returned by microsoft academic rest api: JSON Example: { " ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    I found found several question's answers similar to this, I tried some of them, but no luck. May ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I found found several question's answers similar to this, I tried some of them, but no luck. May ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    How can I get the td values with jquery? The while loops will generate a few with the same class ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    Should I learn JavaScript or Python? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I write a lot of dynamically generated content (developing under PHP) and I use jQuery to add extra ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I am trying to convert string into integer using jquery but it throwing NumberFormatException. How to fix it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    Multiple variables can be created and initialized in a single JavaScript statement 1. False 2. True...
asked Feb 23, 2021 by JackTerrance
0 votes
    var str = ' Header Paragraph I have a string str I want to separate h1 and p text. Result: var ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
...