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

                                        SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                        은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현