JavaScript Date Methods


1. getDay(): Returns the day of the week for the specified date as a number from 0 to 6.

Syntax:

    dateObj.getDay();


Example:

    var d = new Date();
    document.write(d.getDay()); // prints current day


2. getDate(): Returns the day for the specified date as a number from 1 to 31.

Syntax:

    dateObj.getDate();


Example:

    var d = new Date();
    document.write(d.getDate()); // prints current date    


3. getTime(): Returns the number of milliseconds since January 1, 1970.

Syntax:

    dateObj.getTime();


Example:

    var d = new Date();
    document.write(d.getTime()); // prints number of milliseconds from January 1, 1970 till today.    


4. getFullYear(): Returns the year of a date as a 4 digit number.

Syntax:

    dateObj.getFullYear();


Example:

    var d = new Date();
    document.write(d.getFullYear()); // prints current full year i.e. 2018


5. getMonth(): Returns the month of a date as a number from 0 to 11.

Syntax:

    dateObj.getMonth();


Example:

    var d = new Date();
    document.write(d.getMonth()); // prints current month    



6. getHours(): Returns the hours of a date as a number from 0 to 23.

Syntax:

    dateObj.getHours();


Example:

    var d = new Date();
    document.write(d.getHours()); // prints current hours    


7. getMinutes(): Returns the minutes of a date as a number from 0 to 59.

Syntax:

    dateObj.getMinutes();


Example:

    var d = new Date();
    document.write(d.getMinutes()); // prints current minutes    


8. getSeconds(): Returns the seconds of a date as a number from 0 to 59.

Syntax:

    dateObj.getSeconds();


Example:

    var d = new Date();
    document.write(d.getSeconds()); // prints current seconds


9. getMilliseconds(): Returns the milliseconds of a date as a number from 0 to 999.

Syntax:

    dateObj.getMilliseconds();


Example:

    var d = new Date();
    document.write(d.getMilliseconds()); // prints current milliseconds    


10. setDate(): Sets the day of a date object.

Syntax:

    dateObj.setDate(num);


Example:

    var d = new Date();
    document.write(d.setDate(14)); // sets day to 14    



11. setTime(): Sets the time (milliseconds from January 1, 1970) of a date object.

Syntax:

    dateObj.setTime(ms);


Example:

    var d = new Date();
    document.write(d.setTime(14000)); // sets milliseconds to 14000    


12. setFullYear(): Sets the year of a date object.

Syntax:

    dateObj.setFullYear(num);


Example:

    var d = new Date();
    document.write(d.setFullYear(2025)); // sets year to 2025    


13. setMonth(): Sets the month of a date object.

Syntax:

    dateObj.setMonth(num);


Example:

    var d = new Date();
    document.write(d.setMonth(5)); // sets month to 5 i.e. June    


14. setHours(): Sets the hours of a date object.

Syntax:

    dateObj.setHours(num);


Example:

    var d = new Date();
    document.write(d.setHours(15)); // sets hours to 15    


15. setMinutes(): Sets the minutes of a date object.

Syntax:

    dateObj.setMinutes(num);


Example:

    var d = new Date();
    document.write(d.setMinutes(25)); // sets minutes to 25



16. setSeconds(): Sets the seconds of a date object.

Syntax:

    dateObj.setSeconds(num);


Example:

    var d = new Date();
    document.write(d.setSeconds(25)); // sets seconds to 25


17. setMilliseconds(): Sets the milliseconds of a date object.

Syntax:

    dateObj.setMilliseconds(num);


Example:

    var d = new Date();
    document.write(d.setMilliseconds(2500)); // sets milliseconds to 2500