NodeJS add two hours to date?









up vote
3
down vote

favorite
1












I've got a date string as such:



Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)



How can I add two hours to this?



So I get:



Tue Jul 29 2014 01:44:06 GMT+0000 (UTC)










share|improve this question

















  • 2




    I would use moment.js
    – Edwin Dalorzo
    Jul 30 '14 at 0:15










  • i'm guessing it should be July 30?
    – go-oleg
    Jul 30 '14 at 0:56










  • @user3490755 check it out.
    – Piotr Tomasik
    Jul 30 '14 at 2:13














up vote
3
down vote

favorite
1












I've got a date string as such:



Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)



How can I add two hours to this?



So I get:



Tue Jul 29 2014 01:44:06 GMT+0000 (UTC)










share|improve this question

















  • 2




    I would use moment.js
    – Edwin Dalorzo
    Jul 30 '14 at 0:15










  • i'm guessing it should be July 30?
    – go-oleg
    Jul 30 '14 at 0:56










  • @user3490755 check it out.
    – Piotr Tomasik
    Jul 30 '14 at 2:13












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I've got a date string as such:



Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)



How can I add two hours to this?



So I get:



Tue Jul 29 2014 01:44:06 GMT+0000 (UTC)










share|improve this question













I've got a date string as such:



Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)



How can I add two hours to this?



So I get:



Tue Jul 29 2014 01:44:06 GMT+0000 (UTC)







node.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 29 '14 at 23:49









user3490755

3601423




3601423







  • 2




    I would use moment.js
    – Edwin Dalorzo
    Jul 30 '14 at 0:15










  • i'm guessing it should be July 30?
    – go-oleg
    Jul 30 '14 at 0:56










  • @user3490755 check it out.
    – Piotr Tomasik
    Jul 30 '14 at 2:13












  • 2




    I would use moment.js
    – Edwin Dalorzo
    Jul 30 '14 at 0:15










  • i'm guessing it should be July 30?
    – go-oleg
    Jul 30 '14 at 0:56










  • @user3490755 check it out.
    – Piotr Tomasik
    Jul 30 '14 at 2:13







2




2




I would use moment.js
– Edwin Dalorzo
Jul 30 '14 at 0:15




I would use moment.js
– Edwin Dalorzo
Jul 30 '14 at 0:15












i'm guessing it should be July 30?
– go-oleg
Jul 30 '14 at 0:56




i'm guessing it should be July 30?
– go-oleg
Jul 30 '14 at 0:56












@user3490755 check it out.
– Piotr Tomasik
Jul 30 '14 at 2:13




@user3490755 check it out.
– Piotr Tomasik
Jul 30 '14 at 2:13












2 Answers
2






active

oldest

votes

















up vote
7
down vote













Here's one solution:



var date = new Date('Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)').getTime();
date += (2 * 60 * 60 * 1000);
console.log(new Date(date).toUTCString());
// displays: Wed, 30 Jul 2014 01:44:06 GMT


Obviously once you have the (new) date object, you can format the output to your liking if the native Date functions do not give you what you need.






share|improve this answer




















  • Note that (at least in my chrome console) Date(foo) ignores the value of foo and just returns the current Date, whereas new Date(foo) returns a date based on the value of foo, where foo is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
    – cedricdlb
    Aug 10 at 19:03

















up vote
0
down vote













Using MomentJS:



var moment = require('moment');

var date1 = moment("Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)");

//sets an internal flag on the moment object.
date1.utc();

console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));

//adds 2 hours
date1.add(2, 'h');

console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));


Prints out the following:



Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)



Wed Jul 30 2014 01:44:06 GMT+0000 (UTC)






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',
    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%2f25026730%2fnodejs-add-two-hours-to-date%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote













    Here's one solution:



    var date = new Date('Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)').getTime();
    date += (2 * 60 * 60 * 1000);
    console.log(new Date(date).toUTCString());
    // displays: Wed, 30 Jul 2014 01:44:06 GMT


    Obviously once you have the (new) date object, you can format the output to your liking if the native Date functions do not give you what you need.






    share|improve this answer




















    • Note that (at least in my chrome console) Date(foo) ignores the value of foo and just returns the current Date, whereas new Date(foo) returns a date based on the value of foo, where foo is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
      – cedricdlb
      Aug 10 at 19:03














    up vote
    7
    down vote













    Here's one solution:



    var date = new Date('Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)').getTime();
    date += (2 * 60 * 60 * 1000);
    console.log(new Date(date).toUTCString());
    // displays: Wed, 30 Jul 2014 01:44:06 GMT


    Obviously once you have the (new) date object, you can format the output to your liking if the native Date functions do not give you what you need.






    share|improve this answer




















    • Note that (at least in my chrome console) Date(foo) ignores the value of foo and just returns the current Date, whereas new Date(foo) returns a date based on the value of foo, where foo is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
      – cedricdlb
      Aug 10 at 19:03












    up vote
    7
    down vote










    up vote
    7
    down vote









    Here's one solution:



    var date = new Date('Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)').getTime();
    date += (2 * 60 * 60 * 1000);
    console.log(new Date(date).toUTCString());
    // displays: Wed, 30 Jul 2014 01:44:06 GMT


    Obviously once you have the (new) date object, you can format the output to your liking if the native Date functions do not give you what you need.






    share|improve this answer












    Here's one solution:



    var date = new Date('Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)').getTime();
    date += (2 * 60 * 60 * 1000);
    console.log(new Date(date).toUTCString());
    // displays: Wed, 30 Jul 2014 01:44:06 GMT


    Obviously once you have the (new) date object, you can format the output to your liking if the native Date functions do not give you what you need.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 30 '14 at 1:41









    mscdex

    71.6k1210695




    71.6k1210695











    • Note that (at least in my chrome console) Date(foo) ignores the value of foo and just returns the current Date, whereas new Date(foo) returns a date based on the value of foo, where foo is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
      – cedricdlb
      Aug 10 at 19:03
















    • Note that (at least in my chrome console) Date(foo) ignores the value of foo and just returns the current Date, whereas new Date(foo) returns a date based on the value of foo, where foo is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
      – cedricdlb
      Aug 10 at 19:03















    Note that (at least in my chrome console) Date(foo) ignores the value of foo and just returns the current Date, whereas new Date(foo) returns a date based on the value of foo, where foo is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
    – cedricdlb
    Aug 10 at 19:03




    Note that (at least in my chrome console) Date(foo) ignores the value of foo and just returns the current Date, whereas new Date(foo) returns a date based on the value of foo, where foo is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
    – cedricdlb
    Aug 10 at 19:03












    up vote
    0
    down vote













    Using MomentJS:



    var moment = require('moment');

    var date1 = moment("Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)");

    //sets an internal flag on the moment object.
    date1.utc();

    console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));

    //adds 2 hours
    date1.add(2, 'h');

    console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));


    Prints out the following:



    Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)



    Wed Jul 30 2014 01:44:06 GMT+0000 (UTC)






    share|improve this answer


























      up vote
      0
      down vote













      Using MomentJS:



      var moment = require('moment');

      var date1 = moment("Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)");

      //sets an internal flag on the moment object.
      date1.utc();

      console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));

      //adds 2 hours
      date1.add(2, 'h');

      console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));


      Prints out the following:



      Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)



      Wed Jul 30 2014 01:44:06 GMT+0000 (UTC)






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        Using MomentJS:



        var moment = require('moment');

        var date1 = moment("Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)");

        //sets an internal flag on the moment object.
        date1.utc();

        console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));

        //adds 2 hours
        date1.add(2, 'h');

        console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));


        Prints out the following:



        Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)



        Wed Jul 30 2014 01:44:06 GMT+0000 (UTC)






        share|improve this answer














        Using MomentJS:



        var moment = require('moment');

        var date1 = moment("Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)");

        //sets an internal flag on the moment object.
        date1.utc();

        console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));

        //adds 2 hours
        date1.add(2, 'h');

        console.log(date1.format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (UTC)"));


        Prints out the following:



        Tue Jul 29 2014 23:44:06 GMT+0000 (UTC)



        Wed Jul 30 2014 01:44:06 GMT+0000 (UTC)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 12:01









        Adam Matan

        48k90249405




        48k90249405










        answered Jul 30 '14 at 2:06









        Piotr Tomasik

        7,08133451




        7,08133451



























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f25026730%2fnodejs-add-two-hours-to-date%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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto