JavaScript Get the current time and date. TempusJS - working with dates in javascript Method for converting a string to a date

In this lesson, we will become familiar with the JavaScript Date object and learn how to use it in practice.

Creating a Date - 4 Examples

In JavaScript, creating a date is done using the Date object. A Date object represents a point on the time axis and is designed to store a date and time with millisecond precision.

Examples of creating a date in JavaScript.

1. Create the current date and time.

Getting the current date and time in JavaScript is done by instantiating a Date object without specifying any parameters:

// current date (date and time that was at the time the Date object instance was created on the user's local computer) var now = new Date(); // for example, print the current date to the console console.log(now);

If you need to get only today's date in string format, you can use the toLocaleDateString method:

Var now = new Date().toLocaleDateString(); // 12/19/2019

The user's current time can be obtained like this:

Var now = new Date().toLocaleTimeString(); // 11:02:48 var now = new Date().toLocaleTimeString().slice(0,-3); // 11:02

You can get the date and time in string format like this:

Var now = new Date().toLocaleString(); // 12/19/2019, 11:02:48

2. Creating a date by specifying to the Date object the number of milliseconds that have passed since January 1, 1970 00:00:00 UTC.

// 1 year (not high-grade) = 365*24*60*60*1000 = 31536000000 ms // for example, create a date 01/01/1971, 00:00:00 UTC: var date1 = new Date(31536000000);

3. Creating a date by specifying it to a Date object as a string.

With this option for creating a date, JavaScript will try to understand the string passed to it and generate a date based on it. Converting a string to a date in JavaScript is done using the Date.parse method.

For example:

// creating a date based on a string in DD.MM.YY format var date1 = new Date("05.11.19"); // create a date based on a string in the format YYYY-MM-DDThh:mm:ss.sss (the T character is used to separate the date and time) var date2 = new Date("2015-02-24T21:23"); // create a date based on a string indicating the time zone (format YYYY-MM-DDThh:mm:ss.sss±hh:mm): var date3 = new Date("2015-02-24T22:02+03:00") ;

4. Creating a date by specifying the following parameters separated by commas: year (4 digits), month (counting from 0), day (1..31), hours (0..23), minutes (0..59), seconds (0..59), milliseconds (0..999). Moreover, only the first two parameters are mandatory.

An example of creating a date specifying only the required parameters:

// create the date 01/01/2015 (unspecified default parameters are: number - 01, hours - 00, minutes - 00, seconds - 00, milliseconds - 000). var date1 = new Date(2015.01); // create the date 01/24/2015, 21:23 var date2 = new Date(2015,01,24,21,23);

Note: If you need to set the date and time in UTC, you can use the Date.UTC method.

//1 example var date1 = Date.UTC(2015,1,1); var date2 = new Date(date1); alert(date2.toUTCString()); //2 example var newDate = new Date(Date.UTC(2015,1,1)); alert(newDate.toUTCString());

Retrieving individual date and time components

In JavaScript, the following methods are used to obtain individual date and time components:

  • getFullYear() – returns the year consisting of 4 numbers;
  • getMonth() – returns the month in the format of a number from 0 to 11 (0 – January, 1 – February, 2 – March, ..., 11 – December);
  • getDate() – returns the day of the month from 1 to 31;
  • getHours() – returns the number of hours from 0 to 23;
  • getMinutes() – returns the number of minutes from 0 to 59;
  • getSeconds() – returns the number of seconds from 0 to 59;
  • getMilliseconds() – returns the number of milliseconds from 0 to 999.

All of these methods return individual date and time components according to the time zone set on the user's local device.

// create the date 11/11/2019 00:00 UTC var newDate = new Date(Date.UTC(2019,11,11)); // get the date components if the local time on the user's device is UTC+10:00 newDate.getFullYear(); //2019 newDate.getMonth(); // 10 newDate.getDate(); // 11 newDate.getHours(); // 10 newDate.getMinutes(); // 0 newDate.getSeconds(); // 0 newDate.getMilliseconds(); // 0

An example in which we will greet the user depending on what time interval he currently has:

// get the user's current time and the components of this time var now = new Date(), hour = now.getHours(), minute = now.getMinutes(), second = now.getSeconds(), message = ""; // define a greeting phrase depending on the user's local time if (hour

Share with friends or save for yourself:

Loading...