⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

JavaScript Date and Time

JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain an integer representing the number of milliseconds since midnight at the start of January 1, 1970, UTC. A date in JavaScript is essentially defined as time in milliseconds. This timestamp is independent of the time zone and uniquely defines a moment in history. The largest timestamp that a Date object can represent is slightly less than the largest safe integer.

EXAMPLE
RESULT

The Date object can represent up to 8,640,000,000,000,000 milliseconds or 100 million days. This is a range from April 20, 271,821 BC to September 13, 275,760 AD. Any attempt to represent a time outside this range results in a Date object with a timestamp value of NaN, which is an invalid date. JavaScript will default to using the browser's time zone and display the date as a full text string.

Creating and Working with Date Objects

JavaScript allows creating date objects in several ways: with the current date, from a string, from individual values (year, month, day), or from milliseconds. Each date object has numerous methods for retrieving and setting individual components of the date and time.

Methods for Working with Date and Time

Various methods are available for working with dates and times in JavaScript, divided into several categories:

  1. Static methods for creating and converting dates (now, parse, UTC).
  2. Methods for retrieving individual date components (day, month, year, hour, minute, second).
  3. Methods for setting individual date and time components.
  4. Methods for working with UTC (Coordinated Universal Time).
  5. Methods for converting dates into various string formats.

Overview of Date and Time Methods

The table below shows all major methods and properties of the Date object in JavaScript, categorized by type (creation, retrieval, setting, UTC, conversions). For each method, the description of operation, syntax, and a concrete code example are provided.

Name Description Syntax Example
new Date() Creates a new date object with the current date and time. new Date() let now = new Date();
new Date(string) Creates a date object from a date string. new Date("string") let d = new Date("2024-01-15");
new Date(year, month, ...) Creates a date object from individual values (month is 0-11). new Date(year, month, day, hour, min, sec, ms) let d = new Date(2024, 0, 15, 10, 30, 0);
new Date(ms) Creates a date object from milliseconds since January 1, 1970. new Date(milliseconds) let d = new Date(1705312200000);
Date.now() Returns the number of milliseconds since January 1, 1970. Date.now() let ms = Date.now();
Date.parse() Parses a date string and returns milliseconds since January 1, 1970. Date.parse("string") let ms = Date.parse("2024-01-15");
Date.UTC() Returns milliseconds since January 1, 1970 in UTC time. Date.UTC(year, month, day) let ms = Date.UTC(2024, 0, 15);
getDate() Returns the day of the month (1–31). date.getDate() let day = now.getDate();
getDay() Returns the day of the week (0=Sunday, 6=Saturday). date.getDay() let dayOfWeek = now.getDay();
getFullYear() Returns the year (4 digits). date.getFullYear() let year = now.getFullYear();
getHours() Returns the hours (0–23). date.getHours() let hours = now.getHours();
getMilliseconds() Returns the milliseconds (0–999). date.getMilliseconds() let ms = now.getMilliseconds();
getMinutes() Returns the minutes (0–59). date.getMinutes() let min = now.getMinutes();
getMonth() Returns the month (0=January, 11=December). date.getMonth() let month = now.getMonth();
getSeconds() Returns the seconds (0–59). date.getSeconds() let sec = now.getSeconds();
getTime() Returns milliseconds since January 1, 1970. date.getTime() let ms = now.getTime();
getTimezoneOffset() Returns the difference between UTC and local time in minutes. date.getTimezoneOffset() let offset = now.getTimezoneOffset();
setDate() Sets the day of the month (1–31). date.setDate(day) now.setDate(15);
setFullYear() Sets the year (can also set month and day). date.setFullYear(year) now.setFullYear(2025);
setHours() Sets the hours (0–23). date.setHours(hour) now.setHours(14);
setMilliseconds() Sets the milliseconds (0–999). date.setMilliseconds(ms) now.setMilliseconds(500);
setMinutes() Sets the minutes (0–59). date.setMinutes(min) now.setMinutes(30);
setMonth() Sets the month (0–11). date.setMonth(month) now.setMonth(5);
setSeconds() Sets the seconds (0–59). date.setSeconds(sec) now.setSeconds(0);
setTime() Sets the date in milliseconds since January 1, 1970. date.setTime(ms) now.setTime(1705312200000);
getUTCDate() Returns the day of the month in UTC (1–31). date.getUTCDate() let day = now.getUTCDate();
getUTCDay() Returns the day of the week in UTC (0–6). date.getUTCDay() let day = now.getUTCDay();
getUTCFullYear() Returns the year in UTC. date.getUTCFullYear() let year = now.getUTCFullYear();
getUTCHours() Returns the hours in UTC (0–23). date.getUTCHours() let hours = now.getUTCHours();
getUTCMilliseconds() Returns the milliseconds in UTC (0–999). date.getUTCMilliseconds() let ms = now.getUTCMilliseconds();
getUTCMinutes() Returns the minutes in UTC (0–59). date.getUTCMinutes() let min = now.getUTCMinutes();
getUTCMonth() Returns the month in UTC (0–11). date.getUTCMonth() let month = now.getUTCMonth();
getUTCSeconds() Returns the seconds in UTC (0–59). date.getUTCSeconds() let sec = now.getUTCSeconds();
setUTCDate() Sets the day of the month in UTC. date.setUTCDate(day) now.setUTCDate(15);
setUTCFullYear() Sets the year in UTC. date.setUTCFullYear(year) now.setUTCFullYear(2025);
setUTCHours() Sets the hours in UTC. date.setUTCHours(hour) now.setUTCHours(14);
setUTCMilliseconds() Sets the milliseconds in UTC. date.setUTCMilliseconds(ms) now.setUTCMilliseconds(500);
setUTCMinutes() Sets the minutes in UTC. date.setUTCMinutes(min) now.setUTCMinutes(30);
setUTCMonth() Sets the month in UTC. date.setUTCMonth(month) now.setUTCMonth(5);
setUTCSeconds() Sets the seconds in UTC. date.setUTCSeconds(sec) now.setUTCSeconds(0);
toString() Returns the date as a string. date.toString() now.toString(); // "Mon Jan 15 2024 ..."
toDateString() Returns only the date part as a string. date.toDateString() now.toDateString(); // "Mon Jan 15 2024"
toTimeString() Returns only the time part as a string. date.toTimeString() now.toTimeString(); // "10:30:00 GMT+0100"
toISOString() Returns the date in ISO 8601 format. date.toISOString() now.toISOString(); // "2024-01-15T09:30:00.000Z"
toUTCString() Returns the date as a UTC string. date.toUTCString() now.toUTCString(); // "Mon, 15 Jan 2024 09:30:00 GMT"
toLocaleDateString() Returns the date in local format. date.toLocaleDateString() now.toLocaleDateString(); // "1/15/2024"
toLocaleTimeString() Returns the time in local format. date.toLocaleTimeString() now.toLocaleTimeString(); // "10:30:00 AM"
toLocaleString() Returns the date and time in local format. date.toLocaleString() now.toLocaleString(); // "1/15/2024, 10:30:00 AM"
toJSON() Returns the date as a JSON string (same as ISO format). date.toJSON() now.toJSON(); // "2024-01-15T09:30:00.000Z"
valueOf() Returns the primitive value (milliseconds). date.valueOf() now.valueOf(); // 1705312200000
constructor Returns the function that created the Date object prototype. date.constructor now.constructor; // function Date() { ... }
Date.prototype Allows adding properties and methods to the Date object. Date.prototype.newMethod = function() { ... } Date.prototype.greeting = function() { return this.getFullYear(); };