JavaScript Date Features

Modern website - dynamic content and active structure. An effective and simple tool for this is JavaScript + AJAX + Server (PHP).

Using the date / time functions (JavaScript Date) and timer (setInterval) for its intended purpose is natural. If you apply them as a tool for managing processes inside the browser and communicating with the server, the site pages can be revitalized, which will provide comfort to the visitor and increase the rating of the resource.

javascript date

JavaScript function: new Date ()

Function syntax:

  • var cDate = new Date ([parameter | parameters]);

The result is the cDate date object. If the function is called without parameters, it returns the current date object as a result.

A numeric parameter is allowed (only when it is one), which is interpreted as the number of milliseconds elapsed since 01/01/1970 (00:00:00 GMT).

You can use strings and numbers as parameters. For example, "21 January 1965" or "13 May 2002". Both options work, although one is up to the point when the Unix era began.

If you specify the desired date exactly: "year, month, [date, [hour, [minutes, [seconds]]]], milliseconds, then in this case from two to seven digits are passed to the function constructor.For example, '2013,10,28,13,47,12,18' - here quotes are to indicate the list of parameters: the object of the date will be November 28, 2013, time 13: 47: 12.18.

You should carefully record the sequence of numbers and do not forget that January is 0, and December is 11. It is also important to remember that one number as a parameter is time in milliseconds, and two numbers or more are date numbers.

javascript new date

JavaScript function: setInterval (f | k, and)

Very useful language feature. As parameters, 'f | k' is a function or code, and as 'and' is the value through which the function or code specified by the first parameter will be executed.

The interval value is set on the basis that 1000 is 1 second.

Use in javascript: date, format and setInterval can be like this:

javascript date gettime

This code allows you to create a date and time on the screen that is constantly changing. Here the format is done manually, using the padl functions (the example on the right) and the slice function (an own JavaScript function).

Numeric time equivalent

The date object has a time method, which is written as:

  • JavaScript: date.getTime (). It allows you to convert the time to a single numeric value.

For example, executing the getTime () method on the cDate object will assign the iTime variable a numeric time value in milliseconds:

  • var iTime = cDate.getTime ().

The time value is counted in both directions from the beginning of the "Unix" era: 1.1.1970.

A number equivalent to a date and time allows you to calculate the exact number of hours, days, months. In practical applications, this is important. JavaScript allows you to do this quite simple means.

javascript date functions

Date object methods

In JavaScript, Date has get * and set * methods. The first allows you to receive, and the second - to set the appropriate values. The possibilities are very wide and in everyday practice not all are needed.

Methods for getting the year (getFullYear), month (Month), day (Date), hour (Hours). The names of the methods correspond to English words, and their use is not difficult.

An important point: the date / time object in JavaScript: Date, functions, site pages, AJAX and server has three essential components:

  • date / time on site pages;
  • periodic processes of the site pages;
  • information exchange through AJAX with the server.

The JavaScript Date object itself, with all its methods, is a full-featured language element that is sufficient for any use case. A set of his methods will satisfy the taste of the most sophisticated programmer.

javascript date format

Date, time and periodic page processes

The simplest application, in addition to the traditional output of the current time on the page, is very originally to wish for "good morning", "good day". Although this feature is not enough for anyone, it is not worth forgetting about it.

The time spent on the page does matter. At the same time, the time of leaving the page can be traced by means of the above timer with the required accuracy. This element is important for applications that are in continuous dialogue with the client, for example for:

  • instant message delivery;
  • billing for immediate work;
  • tracking staff time and similar procedures.

An important use for the Date JavaScript object is to enter a password and / or a special code sent by mail or SMS. If the delay is noticeable time, there may be an unauthorized access attempt.

javascript date

Exchange with server: AJAX

AJAX technology has become traditional. It has long been not accepted to write sites that update the entire page. Some seek to create resources from a single page, which over time is converted into the desired form. Naturally, such live sites require a very serious attitude to the JavaScript Date object.

The most direct and accessible that is required from the date object and timer is the tracking of external events. A simple example: the exchange. Tables, quotes, various data are continuously updated.If the site participates in the auction, it must periodically, according to the rules established by the exchange, collect the necessary information.

JavaScript is a great language, but it has important limitations that, for example, PHP on the server is deprived. The simplest thing is to write to the database, read the file, go to the page, and then parse.

Among other things, it is not always advisable to put some algorithms in the form of JavaScript code, even if it is encrypted. Some processes are safely and efficiently performed on the server.

To perform all marked and similar works, the timer is a rather complex structure. It may not always be a single algorithm and must not always work with constant tact. The specifics of the problem may impose a lot of restrictions and conditions.

A significant circumstance follows from the above: the JavaScript Date object is a fully functional system of date and time functions. There is no point in emphasizing its use. Everything is accessible and understandable. Of course, you will have to carefully consider the use of certain methods, to check certain parameters, but the object works flawlessly.

In modern site building it is important to completely different.The date and time should be presented in the context of the task, the processes that surround it, and the specifics of the application. It is also important to take into account the temporary characteristics of the server, client computer and communication lines. Programming is already out of the age when syntax ruled over semantics.javascript new date

Time essentially changed everything, the syntax can now allow even semantics outside its constructions. If the world is represented by objects, using arrays and stack ideas, then objects of date and time will acquire a different meaning.

Related news

JavaScript Date Features image, picture, imagery


JavaScript Date Features 8


JavaScript Date Features 49


JavaScript Date Features 32


JavaScript Date Features 72


JavaScript Date Features 96


JavaScript Date Features 7


JavaScript Date Features 77


JavaScript Date Features 17


JavaScript Date Features 49


JavaScript Date Features 45