All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Javascript Date and Time

You can get date object to get and set date and time. Date objects are created with the new Date( ) as shown below.


Syntax of Creating a Date object

var variableName = new Date();
var variableName = new Date(milliseconds);
var variableName = new Date(dateString);
var variableName = new Date(year, month, day, hours, minutes,
                              seconds, milliseconds);

Example of Date

var todaydate = new Date();
alert(todaydate);


Some General Date Methods

  • Date(): Returns today's date and time.

  • getDate(): Returns the day of the month for the specified date according to local time.

  • getDay(): Returns the day of the week for the specified date according to local time.

  • getFullYear(): Returns the year of the specified date according to local time.

  • getHours(): Returns the hour in the specified date according to local time.

  • getMilliseconds(): Returns the milliseconds in the specified date according to local time.

  • getMinutes(): Returns the minutes in the specified date according to local time.

  • getMonth(): Returns the month in the specified date according to local time.

  • getSeconds(): Returns the seconds in the specified date according to local time.

  • setDate(): Sets the day of the month for a specified date according to local time.

  • setHours(): Sets the hours for a specified date according to local time.

  • setMilliseconds(): Sets the milliseconds for a specified date according to local time.

  • setMinutes(): Sets the minutes for a specified date according to local time.

  • setMonth(): Sets the month for a specified date according to local time.

  • setSeconds(): Sets the seconds for a specified date according to local time.

❮ PreviousNext ❯