Last Updated:

Working with date and time in JavaScript

The term js date is interpreted as meaning that refers to midnight on January 1, 1970. It is often used to calculate certain dates. This feature guarantees the same functionality on different platforms. The temporary value is passed between the systems upon their creation and represents the same moment.

The object supports a number of generic methods, including local time. The world standard for the time indicator is Greenwich Mean Time. Local time is used to run JavaScript on your computer. A special constructor is used to form objects.

Working with date and time in JavaScript

Some programmers working with a JavaScript program are faced with the need to display the time and current date on the screen. Most often, customer references or records in a certain variable are used for this. Work with the date occurs on the basis of the date() function.

Data = new Date();

After entering the necessary information, the following line will appear on the screen:

Tue Mar 24 2015 10:18:19 GMT+0300 (RTZ 2 (winter))

This is the easiest input option, but it is advisable not to use it. There are simpler methods for pulling out the necessary values.

For example, a javascript date format might contain:

  • getFullYear - Getting the Year
  • getMonth - Get Month (1 - 12)
  • getDate - Get the date of the month (1 through 31).
  • getHours — get the hour (0 – 23).
  • getMinutes - Get Minute (0 – 59)
  • getSeconds - Get Seconds (0 - 59)

To enter the current date with a date, the name of the month and year you need:

Data = new Date();
Year = Data.getFullYear();
Month = Data.getMonth();
Day = Data.getDate();

The change of the month is performed according to the following algorithm:

switch (Month)
case 0: fMonth="January"; break;
case 1: fMonth="February"; break;
case 2: fMonth="March"; break;
case 3: fMonth="April"; break;
case 4: fMonth="may"; break;
case 5: fMonth="June"; break;
case 6: fMonth="July"; break;
case 7: fMonth="August"; break;
case 8: fMonth="September"; break;
case 9: fMonth="October"; break;
case 10: fMonth="November"; break;
case 11: fMonth="December"; break;


The time output is as follows:

document.write("Today » + Day + » » + fMonth + » » + Year + » Year");

The result is the following string:

Today 24 March 2015

Time changes according to this principle:

Data = new Date();
Hour = Data.getHours();
Minutes = Data.getMinutes();
Seconds = Data.getSeconds();


document.write ("Current time: » + Hour + ":" + Minutes + ":" + Seconds);

The result is manifested as follows:

Current time: 10:49:36

This is exactly what works with the date and time in this format.

Using data js timestamp in libraries


By using various libraries in their work, programmers expect that they will significantly simplify their task of writing code. This also applies to the time, as well as the date.

A source like MooTools has plenty of features over time and date in JavaScript. The use of code in this case is minimized.

For calendar readings, you can perform manual actions and spend a large amount of time. Otherwise, special libraries are used to reduce the development time. They will allow you to spend precious time creating something new. The most popular services, the most commonly used, can be:

They help to achieve the optimal result in the shortest period of time. In modern programming, there are a large number of ready-made solutions that provide real examples. They are based on the basic elements of object-oriented programming.

Possible tools for working with format js date now


The main convenient property of Date objects is the ability to auto-correct. If you enter knowingly incorrect data, the program will automatically correct everything. This process is used to enter a date that is relatively far from a given period of time.

In addition to everything else, there is the possibility of entering zero and even negative components. The main side effect of this functionality may be that objects can be subtracted. The temporary difference will be indicated in milliseconds.

The principle of benchmarking is based on the reusable launch of the function. It is this action that will help minimize the possibility of the influence of various interference. For example, complex functions can be run about 100 times, but simple ones - 1000. You can get more reliable results if you run the entire test package multiple times.


Almost all browsers, except IE10-, support the modern standard ESCA 402. It allows you to use different methods of editing dates. When you call date.toLocaleString (locale, options), you can specify a change in a certain number of changes.

Among the main functions are parameters for entering a number of input settings that the interpreter uses to form a string.

It is possible to use the services when working with the input method without localization. toString(), toDateString(), toTimeString() allow you to return back the standard view, which depends on the browser. The main requirement for it, put forward by programmers, is human readability.

The toString function allows you to reverse the entire date, and toDateString() and toTimeString() only the date and time.

You can increase your own cross-browser compatibility and flexibility by using a hand-written formatting function or a special library.