JavaScript Date Tutorial with Examples
1. ECMAScript Date
No ADS
Date is a built-in data type in the Javascript language. It is a class to create objects that represent date & time.
The ECMAScript standard requires the Date object to be able to represent any date and time, to millisecond precision, within 100 million days before or after 1/1/1970. This is a range of plus or minus 273785 years, so JavaScript can represent date and time till the year 275755.
In computer science, 12:00 am on January 1st, 1970 is a special time. It is used to start counting time. This special time is called epoch (Computer Age).
See more about the concept of Locale.
- Khái niệm Locale trong khoa học máy tính
- Sử dụng Locale trong NodeJS
See also the list of time zones by country:
2. Methods of Date
No ADS
getDate()
The getDate() method returns date of month of Date object in current Locale. Specifically, it returns one integer ranging [1-31].
getDate-example.js
let date = new Date("December 25, 2017 23:15:00");
var day = date.getDate();
console.log(day); // 25
getDay()
The getDate() method returns the date of week of Date object in current Locale, specifically, one integer ranging [0-6]. 0 corresponds to Sunday, and 1 corresponds to Monday, ...
getDay-example.js
let date = new Date("December 25, 2017 11:15:00");// Monday
var day = date.getDay();
console.log(day); // 1
getFullYear()
The getFullYear() method returns year of Date object in current Locale. Specifically, it returns one integer, for example, 2017.
getFullYear-example.js
let date = new Date("December 25, 2017 11:15:00");// Monday
var year = date.getFullYear();
console.log(year); // 2017
getHours()
The getHours() method returns the time of the Date object in current Locale. Specifically, it returns one integer ranging [0-23].
getHours-example.js
let date = new Date("December 25, 2017 22:10:00");
let hour = date.getHours();
console.log(hour); // 22
getMilliseconds()
The getMilliseconds() method returns milisecond of the Date object in current Locale. Specifically, it returns one integer ranging [0-999].
getMilliseconds-example.js
let now = new Date();
console.log(now);// Ex: 2018-11-25T05:52:20.490Z
console.log("getMilliseconds() : " + now.getMilliseconds()); // 490
getMinutes()
The getMinutes() method returns the minute of the Date object in current Locale. Specifically, it returns one integer ranging [0-59].
getMinutes-example.js
let now = new Date();
console.log(now);// Ex: 2018-11-25T05:58:20.178Z
console.log("getMinutes() : " + now.getMinutes()); // 58
getMonth()
The getMonth() method returns the month of the Date object in current Locale. Specifically, it returns one integer ranging [0-11]. 0 corresponds to January and 1 corresponds to February,..
getMonth-example.js
let now = new Date();
console.log(now);// Ex: 2018-11-25T05:58:20.178Z
let month = now.getMonth(); // 10
console.log(month); // 10
getSeconds()
The getSeconds() method returns the second of the Date object in current Locale. Specifically, it returns one integer ranging [0-59].
getSeconds-example.js
let now = new Date();
console.log(now);// Ex: 2018-11-25T06:06:24.211Z
console.log("getSeconds() : " + now.getSeconds()); // 24
getTime()
The getTime() method returns a period of time in miliseconds from 01/01/1970 00:00:00:000 to the time represented by the Date object.
getTime-example.js
let now = new Date();
console.log(now);// Ex: 2018-11-25T06:14:59.213Z
console.log("getTime() : " + now.getTime()); // 1543126499213
getTimezoneOffset()
The getTimezoneOffset() method returns time zone in minutes for current Locale.
Time-zone depends on Locale. For example, the time zone ofVietnam is UTC+07:00 while the one of Russia is wide spreading 11 timezones.
Your computer can select any Locale. Then the clock on the computer will display the time in this Locale. The getTimezoneOffset () method returns the time zone corresponding to the Locale which you are using.
getTimezoneOffset:
- Vietnam: UTC+07:00 ==> -420
- Japan: UTC+09:00 ==> -540
getTimezoneOffset-example.js
// Vietnam: UTC+07:00
let now = new Date();
let timezoneOffset = now.getTimezoneOffset();
console.log(timezoneOffset); // -420
getUTCDate()
The getUTCDate () method returns the date of the month of the Date object by Universal Time. Specifically, it returns one integer from [1-31].
getUTCDate-example.js
// This example test in Locale: Vietnam UTC+7:00
let date = new Date("December 25, 2017 01:15:00");
let day = date.getDate();
console.log(day); // 25 (Test in Vietnam UTC+7:00)
let utcDay = date.getUTCDate();
console.log(utcDay); // 24 (UTC)
getUTCDay()
The getUTCDay() method returns date of week of the Date object in Universal Time, Specifically, one integer ranging [0-6]. 0 corresponds to Sunday; 1 corresponds to Monday, ...
getUTCDay-example.js
// This example test in Locale: Vietnam UTC+7:00
let date = new Date("December 25, 2017 01:15:00"); // Monday
let day = date.getDay();
console.log(day); // 1
let utcDay = date.getUTCDay();
console.log(utcDay); // 0
getUTCFullYear()
The getUTCFullYear() method returns the year of the Date object in Universal Time . In particular, it returns one integer, for example 2017.
getUTCFullYear-example.js
// This example test in Locale: Vietnam UTC+7:00
let date = new Date("January 01, 2019 01:15:00");
var year = date.getFullYear();
console.log(year); // 2019
var utcYear = date.getUTCFullYear();
console.log(utcYear); // 2018
getUTCHours()
The getHours() method returns the time of the Date object in Universal Time. Specifically, it returns one integer ranging [0-23].
getUTCHours-example.js
// This example test in Locale: Vietnam UTC+7:00
let now = new Date();
console.log(now);// Ex: 2018-11-25T07:29:59.575Z
let hour = now.getHours();
console.log(hour); // 14
let utcHour = now.getUTCHours();
console.log(utcHour); // 7
getUTCMinutes()
The getUTCMinutes() method returns the minute) of the Date object in Universal Time. Specifically, it returns one integer ranging [0-59].
getUTCMinutes-example.js
// This example test in Locale: Vietnam UTC+7:00
let now = new Date();
console.log(now);// Ex: 2018-11-25T07:35:06.442Z
console.log("getUTCMinutes() : " + now.getUTCMinutes()); // 35
getUTCMilliseconds()
The getUTCMilliseconds() method returns the milisecond of the Date object in Universal Time. Specifically, it returns one integer ranging [0-999].
getUTCMilliseconds-example.js
// This example test in Locale: Vietnam UTC+7:00
let now = new Date();
console.log(now);// Ex: 2018-11-25T07:39:03.222Z
console.log("getUTCMilliseconds() : " + now.getUTCMilliseconds()); // 222
getUTCMonth()
The getUTCMonth() method returns the month of the Date object in Universal Time. Specifically, it returns one integer ranging [0-11]. 0 corresponds to January and 1 corresponds to February,..
getUTCMonth-example.js
// This example test in Locale: Vietnam UTC+7:00
let date = new Date("January 01, 2019 01:15:00");
console.log(date);// 2018-12-31T18:15:00.000Z
// January 2019 (Vietnam)
let month = date.getMonth(); // 0
console.log(month); // 0
// December 2018 (UTC)
let utcMonth = date.getUTCMonth(); // 11
console.log(utcMonth); // 11
getUTCSeconds()
The getUTCSeconds() method returns second of the Date object in Universal Time. Specifically, it returns one integer ranging [0-59].
getUTCSeconds-example.js
// This example test in Locale: Vietnam UTC+7:00
let now = new Date();
console.log(now);// Ex: 2018-11-25T06:06:24.211Z
console.log("getUTCSeconds() : " + now.getUTCSeconds()); // 24
setDate(dayValue)
The setDate(dayValue) method sets the date value of a month for the Date object in current Locale. The parameter is an integer ranging [1-31].
Okay, One situation!! Suppose you have a Date object that represents 25/02/2017 (February in 2017 has only 28 days). You set value 31 for "date of month" of that Date object. What will happen? The result is that your Date object will represent March 3st, 2017.
setDate-example.js
let date = new Date("February 25, 2017 23:15:00"); // 25/02/2017
date.setDate(18);
console.log(date); // 2017-02-18T16:15:00.000Z
// ??
date.setDate(31);
console.log(date); // 2017-03-03T16:15:00.000Z
setFullYear(yearValue[, monthValue[, dayValue]])
The setFullYear(...) method sets year, month, date values for the Date object in current Locale.
Parameters:
- yearValue: One integer, for example 2018.
- monthValue: One integer ranging [0-11], 0 corresponds to January, 1 corresponds to February,...
- dayValue: One integer ranging [1-31], representing date of a month.
setFullYear-example.js
let date = new Date();
console.log(date);// Ex: 2017-02-25T16:15:00.000Z
date.setFullYear(2015, 3, 19);
console.log(date); // 2018-12-19T16:15:00.000Z
date.setFullYear(2014, 10);
console.log(date); // 2018-12-25T16:15:00.000Z
date.setFullYear(2013);
console.log(date); // 2019-12-25T16:15:00.000Z
setMonth(monthValue[, dayValue])
The setMonth(...) method sets month, date values for the Date object in current Locale.
Parameters:
- monthValue: One integer ranging [0-11], 0 corresponds to January, 1 corresponds to February,...
- dayValue: One integer ranging [1-31], representing date of month.
setMonth-example.js
let date = new Date();
console.log(date);// Ex: 2018-11-25T17:42:01.751Z
date.setMonth(3, 19);// Month, day of month
console.log(date); // 2018-04-18T17:42:01.751Z
date.setMonth(10); // Month
console.log(date); // 2018-11-18T17:42:01.751Z
setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
The setHours(..) metho sets time, minute, second, milisecon values for the Date object in current Locale.
Parameters:
- hoursValue: An integer ranging [0-23].
- minutesValue: An integer ranging [0-59].
- secondsValue: An integer ranging [0-59].
- msValue: An integer ranging [0-999].
setHours-example.js
let date = new Date();
console.log(date);// Ex: 2018-11-25T17:36:18.955Z
date.setHours(22, 11, 03, 555); // hours, minutes, seconds, milliseconds
console.log(date); // 2018-11-26T15:11:03.555Z
date.setHours(13, 10);// hours, minutes
console.log(date); // 2018-11-26T06:10:03.555Z
setMinutes(minutesValue[, secondsValue[, msValue]])
The setMinutes(..) method sets minute, second, milisecond values for the Date object in current Locale.
Parameters:
- minutesValue: An integer within [0-59].
- secondsValue: An integer within [0-59].
- msValue: An integer within [0-999].
setSeconds(secondsValue[, msValue])
The setSeconds(..) method sets second values, milisecon values for the Date object in current Locale .
Parameters:
- secondsValue: An integer within [0-59].
- msValue: An integer within [0-999].
setMilliseconds(millisecondsValue)
The setMilliseconds(msValue) method sets milisecond value for the Date object in current Locale. The parameter is an integer within [0-999].
setTime(timeValue)
The setTime(timeValue) method helpsDate represent for a new time while timeValue is a period of time calculated in miliseconds from 01/01/1970 00:00:00:000 to such time.
setTime-example.js
var date = new Date( "Aug 28, 2008 23:30:00" );
date.setTime( 5000000 );
console.log( date ); // 1970-01-01T01:23:20.000Z
toDateString()
The toDateString() method returns a string representing date of the Date object that people can read.
toDateString-example.js
let date = new Date( );
var str = date.toDateString();
console.log(str); // Thu Nov 29 2018
toTimeString()
The toTimeString() method returns a string representing the time of the Date object, which people can read.
toTimeString-example.js
var dateobject = new Date(1993, 6, 28, 14, 39, 7);
console.log( dateobject.toTimeString() ); // 14:39:07 GMT+0700 (GMT+07:00)
toString()
The toString() method returns a string representing the Date object, which people can read.
toString-example.js
// This example test in Locale: Vietnam UTC+7:00
var dateobject = new Date(1993, 6, 28, 14, 39, 7);
console.log( dateobject.toString() ); // Wed Jul 28 1993 14:39:07 GMT+0700 (GMT+07:00)
toUTCString()
The toUTCString() method returns a string representing the Date object in Universal Time, which people can read.
toUTCString-example.js
// This example test in Locale: Vietnam UTC+7:00
var dateobject = new Date(1993, 6, 28, 14, 39, 7);
console.log( dateobject.toString() ); // Wed Jul 28 1993 14:39:07 GMT+0700 (GMT+07:00)
console.log( dateobject.toUTCString() ); // Wed, 28 Jul 1993 07:39:07 GMT
toLocaleDateString([locales][, options])
The toLocaleDateString([locales][, options]) method returns a string representing the date of the Date object in Locale and the options in parameters. In case of not specifying Locale in parameters, current Locale will be used.
An example with the toLocaleDateString(..) method running on browser.
toLocaleDateString-example.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>toLocaleDateString()</title>
<script>
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(date.toLocaleDateString('de-DE', options));
// expected output: Donnerstag, 20. Dezember 2012
console.log(date.toLocaleDateString('ar-EG', options));
// expected output: الخميس، ٢٠ ديسمبر، ٢٠١٢
console.log(date.toLocaleDateString('ko-KR', options));
// expected output: 2012년 12월 20일 목요일
</script>
</head>
<body>
Show informations in the Console.
</body>
</html>
toLocaleTimeString( [locales][, options])
The toLocaleTimeString([locales][, options]) method retusns a string representing time of the Date object in Locale and options in parameters. In case of not specifying Locale in parameters, current Locale will be used.
toLocaleTimeString-example.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>toLocaleTimeString()</title>
<script>
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
var options = {} // {hour: "2-digit", minute: "2-digit", second: "2-digit"}
console.log(date.toLocaleTimeString('de-DE', options));
// expected output: 10:00:00
console.log(date.toLocaleTimeString('ar-EG', options));
// expected output: ١٠:٠٠:٠٠ ص
console.log(date.toLocaleTimeString('ko-KR', options));
// expected output: 오전 10:00:00
</script>
</head>
<body>
Show informations in the Console.
</body>
</html>
toLocaleString([locales][, options])
The toLocaleString([locales][, options]) method retusns a string representing time of the Date object in Locale and options in parameters. In case of not specifying Locale in parameters, current Locale will be used.
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
toLocaleString-example.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>toLocaleString()</title>
<script>
var date = new Date(Date.UTC(2012, 11, 20, 3, 20, 59));
// { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric',
// hour: "2-digit", minute: "2-digit", second: "2-digit"}
var options = {}; // Default options
console.log(date.toLocaleString('de-DE', options));
// expected output: 20.12.2012, 10:20:59
console.log(date.toLocaleString('ar-EG', options));
// expected output: ٢٠/١٢/٢٠١٢ ١٠:٢٠:٥٩ ص
console.log(date.toLocaleString('ko-KR', options));
// expected output: 2012. 12. 20. 오전 10:20:59
</script>
</head>
<body>
Show informations in the Console.
</body>
</html>
Note: The toLocaleDateString(..), toLocaleTimeString(..), toLocaleString(..) methods operate not as expected on the NodeJS environment, this issue was discussed on GitHub:
No ADS
ECMAScript, Javascript Tutorials
- Javascript Screen Tutorial with Examples
- Javascript MouseEvent Tutorial with Examples
- Javascript XMLHttpRequest Tutorial with Examples
- JavaScript if else Statement Tutorial with Examples
- JavaScript Promise, Async/Await Tutorial with Examples
- Javascript History API Tutorial with Examples
- Javascript Menubar Tutorial with Examples
- Javascript Location Tutorial with Examples
- JavaScript Boolean Tutorial with Examples
- JavaScript setTimeout and setInterval Function
- Javascript FocusEvent Tutorial with Examples
- JavaScript Functions Tutorial with Examples
- Introduction to Javascript HTML5 Canvas API
- Javascript Navigator Tutorial with Examples
- Javascript URL Encoding Tutorial with Examples
- JavaScript Variables Tutorial with Examples
- Javascript InputEvent Tutorial with Examples
- Javascript HashChangeEvent Tutorial with Examples
- Introduction to Javascript and ECMAScript
- Javascript Window Tutorial with Examples
- Alert, Confirm, Prompt Dialog Box in Javascript
- Javascript WheelEvent Tutorial with Examples
- JavaScript Switch Statement
- Javascript Locationbar Tutorial with Examples
- Highlighting code with SyntaxHighlighter Javascript library
- What are polyfills in programming science?
- Class and inheritance simulation techniques in JavaScript
- Bitwise Operations
- Javascript Geolocation API Tutorial with Examples
- JavaScript Symbols Tutorial with Examples
- Javascript FileReader Tutorial with Examples
- JavaScript JSON Tutorial with Examples
- JavaScript Date Tutorial with Examples
- JavaScript Map Collection Tutorial with Examples
- Javascript Console Tutorial with Examples
- JavaScript Strings Tutorial with Examples
- JavaScript Modules Tutorial with Examples
- Javascript Form Validation Tutorial with Examples
- Javascript ChangeEvent Tutorial with Examples
- JavaScript Number Tutorial with Examples
- The History of Modules in JavaScript
- Javascript Statusbar Tutorial with Examples
- Undertanding JavaScript Iterables and Iterators
- Quickstart with Javascript
- JavaScript Event Handling Tutorial with Examples
- JavaScript void Keyword Tutorial with Examples
- Classes and Objects in JavaScript
- JavaScript Set Collection Tutorial with Examples
- Javascript DragEvent Tutorial with Examples
- JavaScript Loops Tutorial with Examples
- Inheritance and polymorphism in JavaScript
- Javascript Scrollbars Tutorial with Examples
- Parsing XML in Javascript with DOMParser
- Quickstart with JavaScript
- JavaScript Web Cookies Tutorial with Examples
- Javascript KeyboardEvent Tutorial with Examples
- Javascript Fetch API Tutorial with Examples
- JavaScript Error Handling Tutorial with Examples
- Undertanding Duck Typing in JavaScript
- JavaScript Arrays Tutorial with Examples
- JavaScript Regular Expressions Tutorial with Examples
Show More