Begin Web Programming with PHP & MySQL

Advertisements



JavaScript Date Object

The date object stores the date, time and provides methods for date or time management. There are many ways to create a date object in JavaScript.

  • var my_date = new Date(). This stores the current date in a variable called my_date.Now we can access all the methods of the date object from the "my_date" variable.
  • var times = new Date(milliseconds). It creates an object with the time equal to the number of milliseconds (1/1000 of a second) passed after the Jan 1st of 1970 UTC+0.
  • var my_date = new Date(dateString). Eg: my_date = new Date("2022-01-01");
  • var my_date = new Date(year, month, day, hours, minutes, seconds, milliseconds).
Properties of Date Object
Property Description
constructor It returns a reference to the date function that created the object.
prototype It is used to add new properties and methods to a date object.

Methods of Date Object
Method Description
getDate() It returns the date of the month according to local time.
getDay() It returns the day of the week according to local time.(Eg: 0 for Sunday, 1 for Monday)
getFullYear() It returns the year of a given date according to local time.
getHours() It returns the hour of a given date according to local time (24 hours clock).
getMilliseconds() It returns the milliseconds from the current time according to local time.
getMinutes() It returns the minutes from the current time according to local time.
getMonth() It returns the month of a given date according to local time.(Eg: 0 fo Jan, 1 for Feb…,11 for Dec)
getSeconds() It returns the seconds from the current time according to local time.
getTime() It returns the milliseconds of a given date according to local time.
getTimezoneOffset() It returns the difference between local time and Greenwich Mean Time (GMT) in minutes.
setDate() It sets the day of the month of a date.
setFullYear() It sets the full year for a given date according to local time.
setHours() It sets the hours for a given date according to local time.
setMilliseconds() It sets the milliseconds for a given date according to local time.
setMinutes() It sets the minutes for a given date according to local time.
setMonth() It sets the month for a given date according to local time.
setSeconds() It sets the seconds for a given date according to local time.
setTime() It sets the value of a Date object according to local time.
toString() It converts a date to a string.
UTC() It returns the number of milliseconds in a Date object since January 1, 1970, 00:00:00, universal time.