Javascript date format like ISO but local












8















how do I format a javascript date like ISO format, but in local time?



with myDate.toISOString() I am getting the time as: "2012-09-13T19:12:23.826Z"



but here, it is 22:13, so how do I include the timezone in above format?





I ended up doing...



pad=function(e,t,n){n=n||"0",t=t||2;while((""+e).length<t)e=n+e;return e}
c = new Date()
c.getFullYear()+"-"+pad(c.getMonth()+1)+"-"+pad(c.getDate()-5)+"T"+c.toLocaleTimeString().replace(/D/g,':')+"."+pad(c.getMilliseconds(),3)









share|improve this question





























    8















    how do I format a javascript date like ISO format, but in local time?



    with myDate.toISOString() I am getting the time as: "2012-09-13T19:12:23.826Z"



    but here, it is 22:13, so how do I include the timezone in above format?





    I ended up doing...



    pad=function(e,t,n){n=n||"0",t=t||2;while((""+e).length<t)e=n+e;return e}
    c = new Date()
    c.getFullYear()+"-"+pad(c.getMonth()+1)+"-"+pad(c.getDate()-5)+"T"+c.toLocaleTimeString().replace(/D/g,':')+"."+pad(c.getMilliseconds(),3)









    share|improve this question



























      8












      8








      8








      how do I format a javascript date like ISO format, but in local time?



      with myDate.toISOString() I am getting the time as: "2012-09-13T19:12:23.826Z"



      but here, it is 22:13, so how do I include the timezone in above format?





      I ended up doing...



      pad=function(e,t,n){n=n||"0",t=t||2;while((""+e).length<t)e=n+e;return e}
      c = new Date()
      c.getFullYear()+"-"+pad(c.getMonth()+1)+"-"+pad(c.getDate()-5)+"T"+c.toLocaleTimeString().replace(/D/g,':')+"."+pad(c.getMilliseconds(),3)









      share|improve this question
















      how do I format a javascript date like ISO format, but in local time?



      with myDate.toISOString() I am getting the time as: "2012-09-13T19:12:23.826Z"



      but here, it is 22:13, so how do I include the timezone in above format?





      I ended up doing...



      pad=function(e,t,n){n=n||"0",t=t||2;while((""+e).length<t)e=n+e;return e}
      c = new Date()
      c.getFullYear()+"-"+pad(c.getMonth()+1)+"-"+pad(c.getDate()-5)+"T"+c.toLocaleTimeString().replace(/D/g,':')+"."+pad(c.getMilliseconds(),3)






      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 13 '12 at 20:11







      Billy Moon

















      asked Sep 13 '12 at 19:15









      Billy MoonBilly Moon

      41.3k18100198




      41.3k18100198
























          3 Answers
          3






          active

          oldest

          votes


















          5














          AFAIK you can't format dates in javascript (without using external libraries). The best you could do is "format it yourself". I mean:



          var date = new Date();
          var year = date.getFullYear();
          var month = date......


          var ISOdate = year + "-" + month + "-" + .... ;


          But there are some good libraries that will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");)



          EDIT:



          Moment.js seems the thing you're looking for: http://momentjs.com/






          share|improve this answer
























          • and the month returned by .getMonth() si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!

            – Billy Moon
            Sep 13 '12 at 19:33











          • Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.

            – alexandernst
            Sep 13 '12 at 19:36



















          4














          No library required! For some Date object, e.g. t = new Date()





          • convert the local time zone offset from minutes to milliseconds



            z = t.getTimezoneOffset() * 60 * 1000




          • subtract the offset from t



            tLocal = t-z




          • create shifted Date object



            tLocal = new Date(tLocal)




          • convert to ISO format string



            iso = tLocal.toISOString()




          • drop the milliseconds and zone



            iso = iso.slice(0, 19)




          • replace the ugly 'T' with a space



            iso = iso.replace('T', ' ')




          Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.






          share|improve this answer































            1














            I went with what Denis Howe said, below as a ready made function for convenience.



            Also one fix: in the original answer t-z does not work because t is a Date, not milliseconds.



            function dateToISOLikeButLocal(date) {
            const offsetMs = date.getTimezoneOffset() * 60 * 1000;
            const msLocal = date.getTime() - offsetMs;
            const dateLocal = new Date(msLocal);
            const iso = dateLocal.toISOString();
            const isoLocal = iso.slice(0, 19);
            return isoLocal;
            }


            With this I get the kind of string that needed as a URL parameter:



            "2018-11-16T12:23:50"





            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%2f12413243%2fjavascript-date-format-like-iso-but-local%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              AFAIK you can't format dates in javascript (without using external libraries). The best you could do is "format it yourself". I mean:



              var date = new Date();
              var year = date.getFullYear();
              var month = date......


              var ISOdate = year + "-" + month + "-" + .... ;


              But there are some good libraries that will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");)



              EDIT:



              Moment.js seems the thing you're looking for: http://momentjs.com/






              share|improve this answer
























              • and the month returned by .getMonth() si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!

                – Billy Moon
                Sep 13 '12 at 19:33











              • Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.

                – alexandernst
                Sep 13 '12 at 19:36
















              5














              AFAIK you can't format dates in javascript (without using external libraries). The best you could do is "format it yourself". I mean:



              var date = new Date();
              var year = date.getFullYear();
              var month = date......


              var ISOdate = year + "-" + month + "-" + .... ;


              But there are some good libraries that will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");)



              EDIT:



              Moment.js seems the thing you're looking for: http://momentjs.com/






              share|improve this answer
























              • and the month returned by .getMonth() si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!

                – Billy Moon
                Sep 13 '12 at 19:33











              • Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.

                – alexandernst
                Sep 13 '12 at 19:36














              5












              5








              5







              AFAIK you can't format dates in javascript (without using external libraries). The best you could do is "format it yourself". I mean:



              var date = new Date();
              var year = date.getFullYear();
              var month = date......


              var ISOdate = year + "-" + month + "-" + .... ;


              But there are some good libraries that will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");)



              EDIT:



              Moment.js seems the thing you're looking for: http://momentjs.com/






              share|improve this answer













              AFAIK you can't format dates in javascript (without using external libraries). The best you could do is "format it yourself". I mean:



              var date = new Date();
              var year = date.getFullYear();
              var month = date......


              var ISOdate = year + "-" + month + "-" + .... ;


              But there are some good libraries that will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");)



              EDIT:



              Moment.js seems the thing you're looking for: http://momentjs.com/







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 13 '12 at 19:21









              alexandernstalexandernst

              4,9271351124




              4,9271351124













              • and the month returned by .getMonth() si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!

                – Billy Moon
                Sep 13 '12 at 19:33











              • Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.

                – alexandernst
                Sep 13 '12 at 19:36



















              • and the month returned by .getMonth() si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!

                – Billy Moon
                Sep 13 '12 at 19:33











              • Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.

                – alexandernst
                Sep 13 '12 at 19:36

















              and the month returned by .getMonth() si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!

              – Billy Moon
              Sep 13 '12 at 19:33





              and the month returned by .getMonth() si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!

              – Billy Moon
              Sep 13 '12 at 19:33













              Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.

              – alexandernst
              Sep 13 '12 at 19:36





              Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.

              – alexandernst
              Sep 13 '12 at 19:36













              4














              No library required! For some Date object, e.g. t = new Date()





              • convert the local time zone offset from minutes to milliseconds



                z = t.getTimezoneOffset() * 60 * 1000




              • subtract the offset from t



                tLocal = t-z




              • create shifted Date object



                tLocal = new Date(tLocal)




              • convert to ISO format string



                iso = tLocal.toISOString()




              • drop the milliseconds and zone



                iso = iso.slice(0, 19)




              • replace the ugly 'T' with a space



                iso = iso.replace('T', ' ')




              Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.






              share|improve this answer




























                4














                No library required! For some Date object, e.g. t = new Date()





                • convert the local time zone offset from minutes to milliseconds



                  z = t.getTimezoneOffset() * 60 * 1000




                • subtract the offset from t



                  tLocal = t-z




                • create shifted Date object



                  tLocal = new Date(tLocal)




                • convert to ISO format string



                  iso = tLocal.toISOString()




                • drop the milliseconds and zone



                  iso = iso.slice(0, 19)




                • replace the ugly 'T' with a space



                  iso = iso.replace('T', ' ')




                Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.






                share|improve this answer


























                  4












                  4








                  4







                  No library required! For some Date object, e.g. t = new Date()





                  • convert the local time zone offset from minutes to milliseconds



                    z = t.getTimezoneOffset() * 60 * 1000




                  • subtract the offset from t



                    tLocal = t-z




                  • create shifted Date object



                    tLocal = new Date(tLocal)




                  • convert to ISO format string



                    iso = tLocal.toISOString()




                  • drop the milliseconds and zone



                    iso = iso.slice(0, 19)




                  • replace the ugly 'T' with a space



                    iso = iso.replace('T', ' ')




                  Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.






                  share|improve this answer













                  No library required! For some Date object, e.g. t = new Date()





                  • convert the local time zone offset from minutes to milliseconds



                    z = t.getTimezoneOffset() * 60 * 1000




                  • subtract the offset from t



                    tLocal = t-z




                  • create shifted Date object



                    tLocal = new Date(tLocal)




                  • convert to ISO format string



                    iso = tLocal.toISOString()




                  • drop the milliseconds and zone



                    iso = iso.slice(0, 19)




                  • replace the ugly 'T' with a space



                    iso = iso.replace('T', ' ')




                  Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 1 '18 at 23:59









                  Denis HoweDenis Howe

                  772913




                  772913























                      1














                      I went with what Denis Howe said, below as a ready made function for convenience.



                      Also one fix: in the original answer t-z does not work because t is a Date, not milliseconds.



                      function dateToISOLikeButLocal(date) {
                      const offsetMs = date.getTimezoneOffset() * 60 * 1000;
                      const msLocal = date.getTime() - offsetMs;
                      const dateLocal = new Date(msLocal);
                      const iso = dateLocal.toISOString();
                      const isoLocal = iso.slice(0, 19);
                      return isoLocal;
                      }


                      With this I get the kind of string that needed as a URL parameter:



                      "2018-11-16T12:23:50"





                      share|improve this answer




























                        1














                        I went with what Denis Howe said, below as a ready made function for convenience.



                        Also one fix: in the original answer t-z does not work because t is a Date, not milliseconds.



                        function dateToISOLikeButLocal(date) {
                        const offsetMs = date.getTimezoneOffset() * 60 * 1000;
                        const msLocal = date.getTime() - offsetMs;
                        const dateLocal = new Date(msLocal);
                        const iso = dateLocal.toISOString();
                        const isoLocal = iso.slice(0, 19);
                        return isoLocal;
                        }


                        With this I get the kind of string that needed as a URL parameter:



                        "2018-11-16T12:23:50"





                        share|improve this answer


























                          1












                          1








                          1







                          I went with what Denis Howe said, below as a ready made function for convenience.



                          Also one fix: in the original answer t-z does not work because t is a Date, not milliseconds.



                          function dateToISOLikeButLocal(date) {
                          const offsetMs = date.getTimezoneOffset() * 60 * 1000;
                          const msLocal = date.getTime() - offsetMs;
                          const dateLocal = new Date(msLocal);
                          const iso = dateLocal.toISOString();
                          const isoLocal = iso.slice(0, 19);
                          return isoLocal;
                          }


                          With this I get the kind of string that needed as a URL parameter:



                          "2018-11-16T12:23:50"





                          share|improve this answer













                          I went with what Denis Howe said, below as a ready made function for convenience.



                          Also one fix: in the original answer t-z does not work because t is a Date, not milliseconds.



                          function dateToISOLikeButLocal(date) {
                          const offsetMs = date.getTimezoneOffset() * 60 * 1000;
                          const msLocal = date.getTime() - offsetMs;
                          const dateLocal = new Date(msLocal);
                          const iso = dateLocal.toISOString();
                          const isoLocal = iso.slice(0, 19);
                          return isoLocal;
                          }


                          With this I get the kind of string that needed as a URL parameter:



                          "2018-11-16T12:23:50"






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 10:25









                          antontantont

                          1,4021016




                          1,4021016






























                              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%2f12413243%2fjavascript-date-format-like-iso-but-local%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

                              Xamarin.iOS Cant Deploy on Iphone

                              Glorious Revolution

                              Dulmage-Mendelsohn matrix decomposition in Python