Increment a given date in JavaScript

Given a date, the task is to increment it. To increment a date in javascript, we’re going to discuss few techniques. First few methods to know:

  • JavaScript getDate() method
    This method returns the day of the month (from 1 to 31) for the defined date.
    Syntax:

    Date.getDate()
    

    Return value:
    It returns a number, from 1 to 31, denoting the day of the month

  • JavaScript setDate() method
    This method sets the day of month to the date object.
    Syntax:

    Date.setDate(day)
    

    Parameters:

    • day:This parameter is required. It specifies the integer defining the day of a month. Values expected are 1-31 but less than 1 and greater than 31 values are used appropriately for previous and next month.

    Return value:
    It returns, representing the number of milliseconds between the date object and midnight January 1 1970.

  • JavaScript getTime() method
    This method returns the number of milliseconds between midnight of January 1, 1970 and the specified date.
    Syntax:



    Date.getTime()
    

    Return value:
    It returns a number, representing the number of milliseconds since midnight January 1, 1970.

  • JavaScript setTime() method
    This method set the date and time by adding/subtracting a defines number of milliseconds to/from midnight January 1, 1970.
    Syntax:

    Date.setTime(millisec)
    

    Parameters:

    • millisec:This parameter is required. It specifies the number of milliseconds to be added/subtracted, midnight January 1, 1970

    Return value:
    It returns, representing the number of milliseconds between the date object and midnight January 1 1970.