NodeJS add two hours to date?
up vote
3
down vote
favorite
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
add a comment |
up vote
3
down vote
favorite
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
2
I would use moment.js
– Edwin Dalorzo
Jul 30 '14 at 0:15
i'm guessing it should beJuly 30
?
– go-oleg
Jul 30 '14 at 0:56
@user3490755 check it out.
– Piotr Tomasik
Jul 30 '14 at 2:13
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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
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
node.js
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 beJuly 30
?
– go-oleg
Jul 30 '14 at 0:56
@user3490755 check it out.
– Piotr Tomasik
Jul 30 '14 at 2:13
add a comment |
2
I would use moment.js
– Edwin Dalorzo
Jul 30 '14 at 0:15
i'm guessing it should beJuly 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
add a comment |
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.
Note that (at least in my chrome console)Date(foo)
ignores the value offoo
and just returns the current Date, whereasnew Date(foo)
returns a date based on the value offoo
, wherefoo
is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
– cedricdlb
Aug 10 at 19:03
add a comment |
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)
add a comment |
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.
Note that (at least in my chrome console)Date(foo)
ignores the value offoo
and just returns the current Date, whereasnew Date(foo)
returns a date based on the value offoo
, wherefoo
is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
– cedricdlb
Aug 10 at 19:03
add a comment |
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.
Note that (at least in my chrome console)Date(foo)
ignores the value offoo
and just returns the current Date, whereasnew Date(foo)
returns a date based on the value offoo
, wherefoo
is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
– cedricdlb
Aug 10 at 19:03
add a comment |
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.
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.
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 offoo
and just returns the current Date, whereasnew Date(foo)
returns a date based on the value offoo
, wherefoo
is interpreted as the number of milliseconds since the Unix Epoch Time (1970.01.01T00.00.00 UTC)
– cedricdlb
Aug 10 at 19:03
add a comment |
Note that (at least in my chrome console)Date(foo)
ignores the value offoo
and just returns the current Date, whereasnew Date(foo)
returns a date based on the value offoo
, wherefoo
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 11 at 12:01
Adam Matan
48k90249405
48k90249405
answered Jul 30 '14 at 2:06
Piotr Tomasik
7,08133451
7,08133451
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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