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;








1















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.










share|improve this question




























    1















    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.










    share|improve this question
























      1












      1








      1








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 0:02









      JosueJosue

      61




      61






















          4 Answers
          4






          active

          oldest

          votes


















          2














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








          share|improve this answer
































            0














            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.






            share|improve this answer























            • 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


















            0














            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');





            share|improve this answer






























              0














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





              share|improve this answer























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



                );













                draft saved

                draft discarded


















                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









                2














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








                share|improve this answer





























                  2














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








                  share|improve this answer



























                    2












                    2








                    2







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








                    share|improve this answer















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






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 25 at 0:14









                    Ele

                    26.3k52354




                    26.3k52354










                    answered Mar 25 at 0:10









                    Tomasz KajtochTomasz Kajtoch

                    562513




                    562513























                        0














                        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.






                        share|improve this answer























                        • 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















                        0














                        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.






                        share|improve this answer























                        • 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













                        0












                        0








                        0







                        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.






                        share|improve this answer













                        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.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        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

















                        • 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











                        0














                        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');





                        share|improve this answer



























                          0














                          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');





                          share|improve this answer

























                            0












                            0








                            0







                            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');





                            share|improve this answer













                            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');






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 25 at 0:10









                            Sundar BanSundar Ban

                            464414




                            464414





















                                0














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





                                share|improve this answer



























                                  0














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





                                  share|improve this answer

























                                    0












                                    0








                                    0







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





                                    share|improve this answer













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






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 25 at 0:17









                                    Syrup72Syrup72

                                    2117




                                    2117



























                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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

                                        Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                        Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                        Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript