Loading...
Loading...
00:00:00

Date Object in Javascript

In JavaScript, the Date object represents a single moment in time. It can be used to work with dates and times, perform calculations and manipulations, and format date and time values for display. Here are the methods and properties of the Date object:

  1. Properties:
  • getDate(): Returns the day of the month (from 1-31).
  • getDay(): Returns the day of the week (from 0-6).
  • getFullYear(): Returns the year (four digits).
  • getHours(): Returns the hour (from 0-23).
  • getMilliseconds(): Returns the milliseconds (from 0-999).
  • getMinutes(): Returns the minutes (from 0-59).
  • getMonth(): Returns the month (from 0-11).
  • getSeconds(): Returns the seconds (from 0-59).
  • getTime(): Returns the number of milliseconds since January 1, 1970.
  • getTimezoneOffset(): Returns the time zone difference, in minutes, from current locale (host system settings) to UTC.
  • setDate(): Sets the day of the month (from 1-31).
  • setFullYear(): Sets the year (four digits).
  • setHours(): Sets the hour (from 0-23).
  • setMilliseconds(): Sets the milliseconds (from 0-999).
  • setMinutes(): Sets the minutes (from 0-59).
  • setMonth(): Sets the month (from 0-11).
  • setSeconds(): Sets the seconds (from 0-59).
  • setTime(): Sets the Date object to a specific date and time, in milliseconds since January 1, 1970.
  • toDateString(): Returns the date portion of the Date object as a human-readable string.
  • toLocaleDateString(): Returns the date portion of the Date object as a localized string.
  • toString(): Returns the entire Date object as a human-readable string.
  • toISOString(): Returns the Date object as a string, formatted in ISO-8601 format.
  • valueOf(): Returns the primitive value of a Date object.
  1. Methods:
  • parse(): Parses a string representation of a date, and returns the number of milliseconds since January 1, 1970.
  • UTC(): Returns the number of milliseconds between a specified date and midnight on January 1, 1970, in Coordinated Universal Time (UTC).
  • now(): Returns the number of milliseconds elapsed since January 1, 1970.

Here is an example of using some of the Date object methods:

const now = new Date(); // create a new Date object with the current date and time
console.log(now); // Output: Sun Feb 13 2022 12:45:15 GMT-0800 (Pacific Standard Time)
console.log(now.getDate()); // Output: 13
console.log(now.getDay()); // Output: 0 (Sunday)
console.log(now.getFullYear()); // Output: 2022
console.log(now.getHours()); // Output: 12
console.log(now.getMinutes()); // Output: 45
console.log(now.getSeconds()); // Output: 15

Using the Date object, we can work with dates and times in a flexible and powerful way.