1

i have this datetime format:

Oct 31, 2012 08:59:52

i would like to re-calculate the datetime incremented (for example) of 2 hours or 50 minutes plus how can i do that?

i need to return the same datetime format showed above and not a timestamp!

itsme
  • 48,972
  • 96
  • 224
  • 345

3 Answers3

2
var date = new Date("Oct 31, 2012 08:59:52");
var timeInMillis = date.getTime();

Now that you have time in milliseconds, you can just add the time you want in millis.

Eg: 2 hours? So, 2*60*60*1000 + timeInMillis

var newDate = new Date(2*60*60*1000 + timeInMillis);

If you want to convert your newDate into the original format, which is a long process, you can some guidance from here:

Where can I find documentation on formatting a date in JavaScript?

My pick of the answers would be:

Use MomentJS

Community
  • 1
  • 1
Pulkit Mittal
  • 5,916
  • 5
  • 21
  • 28
1

You could first parse this to a date:

var d=new Date("October 31, 2012 08:59:25").getTime();

Then add the offset:

d+= (seconds)*1000 + (minutes)*60000 + (hours)*3600000;
var result = new Date(d);

I am just not sure wether it accepts 'Oct' instead of 'October'

tobspr
  • 8,200
  • 5
  • 33
  • 46
0
time_start = new Date(year, month, day, hours, minutes, seconds, milliseconds);
time_finish = new Date() - time_start;

Set the Date using the format listed above. To calculate the interval between two points of time, just subtract the current date from the past date.

Jack Guy
  • 8,346
  • 8
  • 55
  • 86