JavaScript Date & Time Functions and Methods


Here's a comprehensive list of date and time functions available in JavaScript, primarily using the Date object. Each function is briefly described for clarity.

JavaScript Date and Time Functions

  1. Date() Constructor

    • new Date(): Creates a new Date object representing the current date and time.
    • new Date(value): Creates a new Date object from a string representation of a date.
    • new Date(milliseconds): Creates a new Date object from the number of milliseconds since January 1, 1970.
  2. Static Methods

    • Date.now(): Returns the number of milliseconds since January 1, 1970.
    • Date.parse(string): Parses a date string and returns the number of milliseconds since January 1, 1970.
    • Date.UTC(year, month, day, hours, minutes, seconds, milliseconds): Returns the number of milliseconds since January 1, 1970, for a specified UTC date.
  3. Instance Methods

    • date.getDate(): Returns the day of the month (1-31) for the specified date.
    • date.getDay(): Returns the day of the week (0-6) for the specified date.
    • date.getFullYear(): Returns the four-digit year (e.g., 2024) for the specified date.
    • date.getHours(): Returns the hour (0-23) for the specified date.
    • date.getMilliseconds(): Returns the milliseconds (0-999) for the specified date.
    • date.getMinutes(): Returns the minutes (0-59) for the specified date.
    • date.getMonth(): Returns the month (0-11) for the specified date.
    • date.getSeconds(): Returns the seconds (0-59) for the specified date.
    • date.getTime(): Returns the number of milliseconds since January 1, 1970.
    • date.getTimezoneOffset(): Returns the time zone offset from UTC in minutes.
    • date.getUTCDate(): Returns the day of the month (1-31) in UTC for the specified date.
    • date.getUTCDay(): Returns the day of the week (0-6) in UTC for the specified date.
    • date.getUTCFullYear(): Returns the four-digit year in UTC for the specified date.
    • date.getUTCHours(): Returns the hour (0-23) in UTC for the specified date.
    • date.getUTCMinutes(): Returns the minutes (0-59) in UTC for the specified date.
    • date.getUTCMonth(): Returns the month (0-11) in UTC for the specified date.
    • date.getUTCSeconds(): Returns the seconds (0-59) in UTC for the specified date.
  4. Setting Methods

    • date.setDate(day): Sets the day of the month for a specified date.
    • date.setFullYear(year, month, day): Sets the full year for a specified date.
    • date.setHours(hours, minutes, seconds, milliseconds): Sets the hour for a specified date.
    • date.setMilliseconds(milliseconds): Sets the milliseconds for a specified date.
    • date.setMinutes(minutes, seconds, milliseconds): Sets the minutes for a specified date.
    • date.setMonth(month, day): Sets the month for a specified date.
    • date.setSeconds(seconds, milliseconds): Sets the seconds for a specified date.
    • date.setTime(milliseconds): Sets the date and time value in milliseconds since January 1, 1970.
  5. String Conversion Methods

    • date.toDateString(): Returns the date portion of a Date object as a string.
    • date.toISOString(): Returns a string in ISO 8601 format (YYYY-MM-DDTHH:mm
      .sssZ).
    • date.toJSON(): Returns a JSON representation of the date.
    • date.toLocaleDateString(locales, options): Returns the date portion in a language-sensitive format.
    • date.toLocaleString(locales, options): Returns a string representing the date and time in a language-sensitive format.
    • date.toLocaleTimeString(locales, options): Returns the time portion in a language-sensitive format.
    • date.toString(): Returns a string representation of the Date object.
    • date.toTimeString(): Returns the time portion of a Date object as a string.
    • date.toUTCString(): Returns a string representing the date in UTC format.
  6. Additional Functions (ES6 and Beyond)

    • Date.prototype.toPrimitive(): Used internally to convert a date to a primitive value.
    • Intl.DateTimeFormat: A constructor for objects that enable language-sensitive date and time formatting.

Summary

JavaScript provides a rich set of functions and methods for working with dates and times. These allow you to create, manipulate, and format dates easily, catering to various needs such as date arithmetic, string representation, and localization.