How to increment date by one day in type script angular 6+

Dung Do Tien Nov 07 2020 42

I'm trying to increase one day for a date variable. Code as below:

var currentDate = new Date(); // Sat Nov 07 2020 10:37:11 GMT+0700
var endDate = new Date(startDate.getDate() + 1); 
alert(endDate); // return -> Thu Jan 01 1970 07:00:00 GMT+0700

I don't know why endDate variable return *Thu Jan 01 1970 07:00:00 GMT+0700* I expect it has to return Thu Jan Sun Nov 08 2020 10:37:11 GMT+0700

Please explain to me if you know any reason.

Have 3 answer(s) found.
  • You can use setDate() method of date object to increment one day. See the example below:

    var currentDate = new Date(); // Mon Nov 08 2020 09:38:46 GMT+0700 (Indochina Time)
    
    currentDate.setDate(currentDate.getDate() + 1);
    
    alert(currentDate); // return -> Mon Nov 09 2020 09:38:46 GMT+0700 (Indochina Time) 

    In angular, Date() object is a datatype of javascript so you can see example here.

    Sandeep Kumar Nov 08 2020
  • It's very easy, you can add n days by way below:

    var days = 1;
    var current = new Date(); // Mon Nov 11 2020 09:46:57 GMT+0700 (Indochina Time)
    var followingDay = new Date(current.getTime() + (days * 24 * 60 * 60 * 1000)); // + n day in ms
    
    alert(followingDay);  // Mon Nov 12 2020 09:46:57 GMT+0700 (Indochina Time) 

    I hope it helpful to you!!!!

    Manish Kumar Nov 08 2020
  • For short code I usually do as below :

    var nextDays = new Date(new Date().setDate(new Date().getDate() + 1)); 
    
    Marry Christ Nov 08 2020
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips