-2

I would like to add 30 minutes to this date format:

"Mon Sep 11 2017 12:00:00 GMT+0200 (CEST)"

I've made a function that works with the ISO 8601:

add30mnTo(date : string){
    var initialdate = (this.datetotimestamp(date) + 1800) * 1000; // 1800 for 30min
    var dateinit = new Date (initialdate)
    var result = dateinit.toISOString();
    alert(result);
    return result;
  }

datetotimestamp(date : string){
    var myDate = new Date(date);
    var withOffset = (myDate.getTime())/1000;
    return withOffset ;
  }

But I don't know how I can modify it to work the full text string format

moonshine
  • 799
  • 2
  • 11
  • 24

1 Answers1

0

Adding 30 mins to a date, you can simply use getTime, and then add the amount of time, and then use setTime to set it. getTime is the time in milliseconds, so adding 30*60*1000, will give you 30mins.

Also note, time is localtime, so the result for me is 2017-09-11T10:30:00.000Z, So it still has added 30 mins, but due to timezones may look different to other people

function add30mnTo(date){
  var d = new Date(date);
  d.setTime(d.getTime() + 30*60*1000);
  return d.toISOString();
}

console.log(add30mnTo("Mon Sep 11 2017 12:00:00 GMT+0200 (CEST)"));
Keith
  • 22,005
  • 2
  • 27
  • 44