JavaScript Print the exact hour that date. TempusJS - time to date in javascript Javascript current hour in milliseconds

Zharoznizhuvalny for children is recognized as a pediatrician. Allegedly, there are situations of inconvenient help for feverish women, if the children are in need of giving innocently. Todi dad take on the versatility and constipation of fever-lowering drugs. How can you give children a breast? How can you beat the temperature of older children? What are the best ones?

At the end of the day, learn with the Date object and move JavaScript that you can learn to practice.

Svorennya dati - 4 butt

JavaScript has a date stem to ask for an additional Date object. The Date object is speckled on the axis of the hour and the designations for the selection of the date and hour from the precision to the millisecond.

Install a JavaScript date.

1. Svvennya streaming date that hour.

I will trim the date that hour in JavaScript. for the additional release of the instance of the Date object without setting parameters:

// streaming date (date is the hour, when the instance of the Date object was closed at local computer koristuvach) var now = new Date (); // for example, I will vivedemo the date console console.log (now);

If you need to correct the current date from the row format, you can speed up the toLocaleDateString method:

Var now = New Date (). ToLocaleDateString (); // 19.12.2019

The current hour of the koristuvach can be modeled as follows:

Var now = New Date (). ToLocaleTimeString (); // 11:02:48 var now = New Date (). ToLocaleTimeString (). Slice (0, -3); // 11:02

The date and hour in the row format can be set as follows:

Var now = New Date (). ToLocaleString (); // 19.12.2019, 11:02:48

2. The date of the date by the way of ordering the date of the number of milliseconds, which passed from the 1st of 1970 p. 00:00:00 UTC.

// 1 рік (not leap year) = 365 * 24 * 60 * 60 * 1000 = 31536000000 ms // for example, the date 01.01.1971, 00:00:00 UTC is set: var date1 = new Date (31536000

3. Date at viglyadі vkazіvka її ob'єktu Date yak row.

With a wide variety of dates, JavaScript will try the intelligence of the transmissions of the 1st row and formulate the date on the basis of the date. Reimplementing a row to a date JavaScript look for the additional method Date.parse.

For example:

// Set date based on the row in DD.MM.YY format var date1 = new Date ("05.11.19"); // set the date on the basis of the row in the format YYYY-MM-DDThh: mm: ss.sss (the symbol T is chosen for the date of the hour) var date2 = new Date ("2015-02-24T21: 23"); // the date is set based on the row from the time zone (format YYYY-MM-DDThh: mm: ss.sss ± hh: mm): var date3 = new Date ("2015-02-24T22: 02 + 03: 00") ;

4. The date for additional assignment of offensive parameters through whom:рік (4 digits), month (view from 0), day (1..31), year (0..23), chilini (0..59), seconds (0..59), milliseconds (0. .999). Moreover, the obovazkovy є deprive the first two parameters.

The butt of the stem date from the values ​​deprived of the binding parameters:

// set the date 01/01/2015 (not specified parameters for the names of the variables: number - 01, date - 00, hvili - 00, seconds - 00, milliseconds - 000). var date1 = New Date (2015.01); // the date is set on 01.24.2015, 21:23 var date2 = New Date (2015,01,24,21,23);

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

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

Otrimannya okremikh components_in date that hour

JavaScript for rejecting some of the components in the date and hour of the start method:

  • getFullYear () - rotate ryk to add up from 4 numbers;
  • getMonth () - rotate the month in the format of the number from 0 to 11 (0 - very, 1 - lute, 2 - birch, ..., 11 - breast);
  • getDate () - rotate the number of months from 1 to 31;
  • getHours () - rotation number of years from 0 to 23;
  • getMinutes () - rotate the number of quills from 0 to 59;
  • getSeconds () - rotate the number of seconds from 0 to 59;
  • getMilliseconds () - rotate number of milliseconds from 0 to 999.

All methods are used to rotate around the time components of the date according to the time zone set on the local attachment of the koristuvach.

// date 11.11.2019 00:00 UTC var newDate = new Date (Date.UTC (2019,11,11)); // otrymaєmo components of the date, as at the annexe of the corystuvach the morning hour is coming UTC + 10: 00 newDate.getFullYear (); // 2019 newDate.getMonth (); // 10 newDate.getDate (); // 11 newDate.getHours (); // 10 newDate.getMinutes (); // 0 newDate.getSeconds (); // 0 newDate.getMilliseconds (); // 0

The butt, in which there is a vitaєmo koristuvach in the presence of that, as at a time at a new interval of the hour:

// the current hour of the user is visible to the component of the hour var now = new Date (), hour = now.getHours (), minute = now.getMinutes (), second = now.getSeconds (), message = ""; // Significantly, the phrase is grateful for a long time at the first hour if (hour<= 6) { message = "Доброе время суток"; } else if (hour <= 12) { message = "Доброе утро"; } else if (hour <= 18) { message = "Добрый день"; } else { message = "Добрый вечер"; } // выполним форматирование времени с использованием тернарного оператора minute = (minute < 10) ? "0" + minute: minute; second = (second < 10) ? "0" + second: second; hour = (hour < 10) ? "0" + hour: hour; message += ", сейчас " + hour + ":" + minute + ":" + second; // выведем приветствие и время в консоль console.log(message); // Добрый вечер, сейчас 22:50:39

At the end of the day, in the required format, it is available from the list of the ternary operator.

JavaScript can be analogs of the same methods for rejecting certain components in dates and times for the UTC + 0 time zone. The names of the methods are the same, let us add "UTC" for "get": getUTCFullYear (), getUTCMonth (), getUTCDate (), getUTCHours (), getUTCMinutes (), getUTCSeconds (), getMilliseconds ().

You can print the day number of the JavaScript run with the help of the getDay () method.

The whole method is rotated the number of vid 0 to 6 (0 - week, 1 - Monday,…, 6 - Saturday).

Butt, for which the day is translated from numeric to row feed:

Var days = ["Nedіlya", "Ponedіlok", "Vіvtorok", "Seredovische", "Chetver", "P'yatnytsya", "Subota"]; // I will fine-tune the date var now = new Date (); // Vivedemo to the console the day of the month console.log ("Christmas" + days);

Rewind the number of milliseconds that have passed since 01/01/1970 00:00:00 UTC in JavaScript, you can use the additional getTime () method.

The difference between the local time zone and UTC in JavaScript can be obtained by using the getTimezoneOffset () method.

Installation of additional components in the date of the hour

In JavaScript, you can set up some of the date components that the hour can be done with the help of the advanced Date object methods:

  • setFullYear (year [, month, date]) - setting the rock (you can set the month beforehand);
  • setMonth (month [, date]) - setting the month from 0 to 11 (0 - very early, 1 - lute, 2 - birch, ..., 11 - breast); additional method of allowing to set the number;
  • setDate (date) - number setting;
  • setHours (hour [, min, sec, ms]) - will set the hour from 0 to 23 (you can set the hour beforehand, seconds and milliseconds);
  • setMinutes (min [,] - set minutes from 0 to 59 (you can set it in less than a second or millisecond);
  • setSeconds (sec,) - will set the seconds from 0 to 59 (you can set it as soon as possible);
  • setMilliseconds (ms) - set miliseconds (from 0 to 999).

All methods are used to set the date to that hour in the time zone set on the computer of the koristuvach.

// Create an instance of the Date object to avenge the exact date var newDate = new Date (); // installable рік newDate.setFullYear (2019); // set up rik and misyats newDate.setFullYear (2019, 08); // can be installed on 20.09.2019 newDate.setFullYear (2019, 08, 20); // set month newDate.setMonth (05); // Settable month that number newDate.setMonth (05, 15); // set the number newDate.setDate (28); // set the time newDate.setHours (13); // Replaceable year and chilin newDate.setHours (13,20);

In JavaScript, setting the date and hour in the time zone UTC + 0 go for additional advanced methods: setUTCFullYear (), setUTCMonth (), setUTCDate (), setUTCHours (), setUTCMinutes (), setUTCSecondes (), setUTCMilliseconds ().

Setting the date one hour after an additional number of milliseconds, passed since 01/01/1970 00:00:00 UTC

Krym, JavaScript doesn’t have to give up the wrong components for an hour, it just stinks automatically.

For example:

// number 44 will be distributed like this: 44 - 31 = 13, 13 of February 2019 newDate.setFullYear (2019, 01, 44);

Tsei priyom can be victorious, if you need to correct the date, as it will be seen from the given at the singing time of the hour.

// date that will be 7 days longer for newDate newDate.setDate (date1.getDate () + 7); // date that you will be less than newDate for 120 seconds newDate.setSeconds (date1.getSeconds () - 120); // so you can set the stop number in the foreground month for newDate newDate.setDate (0);

Reinventing the date for a row and formatting

In JavaScript, methods for converting a date into a row can be applied to two groups.

Prior to the first group, you can introduce methods that behave like the way the browser is assigned: toString (), toDateString (), toTimeString (), toUTCString ().

The toString () method is rotated outside the specified date, toDateString () —––––––––––––––––––––––––––––––––––––––––––––––– again.

Because Rows of detection, as there may be a turn of the methods, clearly not in the standard, can be seen in other browsers.

To another group it is possible to introduce methods according to the time zone of the local computer: toLocaleString () - date that hour, toLocaleDateString () - less date, toLocaleTimeString () - less hour.

Methods toLocaleString (), toLocaleDateString (), toLocaleTimeString () may have 2 optional parameters. The first parameter of values ​​for explicitly specifying a locale, the other for setting an optional format.

If the locale is clearly not set, or undefined, then the browser takes that, as if it were for umovchannyam.

Last but not least, JavaScript is also the toISOString () method. The row is rotated to show the date and hour in the ISO format (YYYY-MM-DDTHH: mm: ss.sssZ).

// the date is set on 04/15/2019 18:43:59 (at the time zone of the koristuvach) var newDate = new Date (2019, 03, 15, 18, 43, 59); console.log (newDate.toString ()); // Mon Apr 15 2019 18:43:59 GMT + 1000 (Vladivostok, standard hour) console.log (newDate.toDateString ()); // Mon Apr 15 2019 console.log (newDate.toTimeString ()); // 18:43:59 GMT + 1000 (Vladivostok, standard hour) console.log (newDate.toLocaleString ()); // 04/15/2019, 18:43:59 console.log (newDate.toLocaleDateString ()); // 04/15/2019 console.log (newDate.toLocaleTimeString ()); // 18:43:59 console.log (newDate.toUTCString ()); // Mon, 15 Apr 2019 08:43:59 GMT console.log (newDate.toISOString ()); // 2019-04-15T08: 43: 59.000Z

Method for rewriting a row for a date

JavaScript for rewriting a row for a date using the Date.parse () method. The whole method can be reconfigured a row, if it is in vikonaniy according to the RFC2822 standard or ISO 8601.

At a certain level, the ISO 8601 standard is readable, at which row the mother is responsible for the offensive format: YYYY-MM-DDThh: mm: ss.sssZ.

  • YYYY - рік, be stored in 4 digits;
  • MM - Misyats, which can be stored in 2 digits (01 = Sichen, 02 = Lutii, etc.);
  • DD - Day of Month, which is stored in 2 digits (01..31);
  • T - the symbol for the date of the hour;
  • hh - number of years (00..23);
  • mm - amount of quiline (00..59);
  • ss - number of seconds (00..59);
  • sss - number of milliseconds (0..999);
  • Z is a character that stands for the hour to be set in UTC format. If you need to change UTC to set the time zone, then the letter Z goes to the values ​​+ hh: mm or -hh.mm.

Since there is a row that the date and hour are not in the RFC2822 format or ISO 8601, the JavaScript Date.parse () method is all one and the same, otherwise the result might not be transferable.

Victory new Date () to kickstart the new Date object, in order to avenge the exact date of that hour.

To brutalize respect, scho Date () viclikana without arguments, equivalent new Date (Date.now ()) .

If you have a date object, you can find out whether there are any available methods for rejecting authority (for example, getFullYear () for rejecting 4-digit rock).

Bottom of the set are the methods of date dating.

Otrimati streaming rik

var year = (new Date ()). getFullYear (); console.log (year); // Sample output: 2016

Otrimat the current month

var month = (new Date ()). getMonth (); console.log (month); // Sample output: 0

Beastly respect, scho 0 = very much. Moreover, the month is changing from 0 before 11 This is often enough to add +1 to the index.

Otrimati the day

var day = (new Date ()). getDate (); console.log (day); // Sample output: 31

Otrimati I will sharpen the year

var hours = (new Date ()). getHours (); console.log (hours); // Sample output: 10

Otrimati the thread

var minutes = (new Date ()). getMinutes (); console.log (minutes); // Sample output: 39

Cut off the current seconds

var seconds = (new Date ()). getSeconds (); console.log (second); // Sample output: 48

Rewrite the current milliseconds

To trim the miliseconds (from 0 to 999) of the instance of the Date object, pick the getMilliseconds method.

Var milliseconds = (new Date ()). GetMilliseconds (); console.log (milliseconds); // Output: milliseconds right now

Reincarnation of the flow hour and date on a row, it's easy to read.

var now = New Date (); // convert date to a string in UTC timezone: console.log (now.toUTCString ()); // Output: Wed, 21 Jun 2017 09:13:01 GMT

The static method Date.now () rotates the number of milliseconds passed since 1 September 1970 00:00:00 UTC. Just correct the number of milliseconds that have passed since that hour, and you have a copy of the Date object, just use the getTime method.

// get milliseconds on the list static method now of Date console.log (Date.now ()); // get milliseconds using method getTime of Date instance console.log ((new Date ()). getTime ());

Ridkisnuyu programmer trap to unique robots with the date that hour. Vzagal, date / hour - the basic understanding of the majority of people є inspired by the mechanisms of robots with a type of tribute. Zavalosya used, JS is not є a blame, є the Date type, є a set of functions in prototypes, prote ...

Hto vinen
The first problem is to determine if you need to set the date / hour for the time zones, from UTC to local. The Date constructor does not need such a parameter.

New Date (); new Date (value); New Date (dateString); new Date (year, month [, day [, hour [, minute [, second [, millisecond]]]]]]);
Single option, you can use UTC - the third way. The designer's Viklik in the whole format allows you to transfer the compensation as a part of the row:

New Date ("Sun Feb 01 1998 00:00:00 GMT + 0700")
The row is accepted in the RFC2822 format, which is not manual and important before manual input. It is practically unwise to trim such a row for the introduction of koristuvach. I can’t tell the person if I had to enter the date in this format.

Uninvolved on those who are in Date, you can set all okremo parameters for the UTC timezone - the problem is not true - the timezone will become local. Ale tse is the only problem.

Zmіshennya schodo UTC is not a constant. The whole function is from the date, the hour (well, or from the timestamp, which is good) and, I know, the time zone. For example, for Moscow, a stopover for an hour is yes:

New Date (2014, 9, 25, 0, 0, 0); // 26.10.2014, 21:00:00 GMT + 3 new Date (2014, 9, 27, 0, 0, 0); // 25.10.2014, 22:00:00 GMT + 4
With such a rank, the designer in the third option is practically marnimous, and some of the honors are needed by the nobility for a long time. And there, yak bulo it is said, you can’t just trimmed butti. One library, as I got it, as a vicarist base for Olson's tribute to the calculation of damage - timezone-JS. The problem with the collection of the library is that the lower libraries (date / time pickers) do not know anything about it and are actively working towards the standard Date. Інші libraries, scho work with the Date object, obviously, the base will not work, and it will not be possible to get it. (Correct me in the comments.)

The business has a time zone and a time zone is set if the date is set on that hour. For example, if it’s a working day to fix about 9:00, it’s unlikely that your colleague from Vladivostok will start at 15:00. The time zone is not guilty of being able to display the date in UTC. However, in the case of regular podіyа, which are displayed at once in different time zones, the timezone is still required. For example, your usual scrum will be repaired about 10:00 for you and about 13:00 for Novosibirsk. Until the speech, at all times and at the time of GMT and UTC. UTC - one hour without usunenny, and GMT - one hour from 0 orders.

12/31/2014, 20:59:59 GMT in the Moscow time zone 12/31/2014, 23:59:59 12/31/2014, 20:59:59 UTC in the Moscow time 12/31/2. : 59
Through qiu arithmetic їх zdebіlshy wander. It's a pity, it's messed up with this parameter. The duration of the direct order of the JS timezone is interpreted as a local timezone, and the timezone UTC and GMT are equivalent.

At the situation of my biopools Intl. Mig bi, ale not goiters. Zokrema, there is such a timeZone parameter, but the standard is set to: The time zone to use. The only value implementations must recognize є "UTC". In Danish hour, except for Chrome, more than one browser of the same time zone will not be available.
In the range of an hour, JS is all shitty - nothing similar in movin '. If you want to do good, you can do it yourself.

Scho robiti
  • Option 1.
    Do not vikoristovuvati enough timezone. The option is short and, melodiously, the most lovable. Tobto, you have a local UTC time zone. For all the glitches in all browsers, everything is є, but not even manual. Until then, the timezone is displayed globally for the OS and for a specific web attachment is not kosher.
  • Option 2.
    As long as the timezone is consumed, it is not just a timestamp. Zovsim. Save an hour at the mercy shop in the RFC line due to the timezone values. There is no way that you can help the destruction of the timezone in the cross-browser pinkie, ale, yak minimum, Chrome about such destruction in the course.
  • Option 3
    Situations are different and trawling so that the hour is to sign up for an attachment. Tobto at the time stamp. Here there is a big nikudi, just to correctly represent the hour is needed by the nobility, either I will add a timezone, or a timezone of a koristuvach, or that one and the same thing, if you can fight all the hand-to-hand destruction. There is no need to do without Olson's victorian.
  • There is no morality of the whole story, but let me leave nothing more. I don’t spite any crumbs with the ECMA standard, melodiously, I won’t.

It's a bit simpler to trim the JavaScript date. For a variety of purposes, the Date. Yogo syntax is simple, and the name of methods is intuitively intelligent. At the hovering lower butts, I will show you, yak pratsyuvati with the Date object ( JavaScript New Date), just correct the flow rate, month, day and hour from the exact moment to the millisecond!

Rejecting the flow date:

So if I can stream the JavaScript date, use the new keyword to extrude a new instance of the Date object.

var date = New Date (); console.log (date);

Send the code to the console as follows:

// Tue Feb 02 2016 15:46:56 GMT-0500 (Eastern Standard Time)

Here you can see the current day of the month, the current month, the day of the month, the ric and the current hour in the 24-year format. Fragment " GMT"Means the hour behind Greenwich, and" -0500 - price difference in time zones between the international time and GMT.

Quote from Vіkіpedії: "The average hour for Grіnvіch ( GMT) - the middle sleepy hour at the Royal Observatory in Greenwich, London. " Tse tied with universesal coordinated hour (UTC). « Eastern Standard Time»At the specified date, the date is entered to the time zone of your browser or computer.

* Beast of respect, that GMT and UTC are different from oneself. We will look at the UTC value at the Khilinas.

* It is also a beast of honor, that the JavaScript date starts in an hour in milliseconds, since it passed since 01 September 1970, UTC. In one day 86,400,000 milliseconds. The range of the Date object is from -100,000,000 days to 100,000,000 days from 01 September 1970 UTC.

* Significance, otrimanі from vikorystannyam vischeznachennogo method, to lay down in the alignment of the system and your outbuilding. As you change the setting of the date of the computer, change the hour, which method will be repeated.

Garazd, we have chosen new Date JavaScript formats and now we are ready to collapse!

Rejection of the day:

Schob otrimati day tizhnya, vikorystovyte method JavaScript Date Object getDay ():

var day = date.getDay (); console.log (day);

* Beastly respect, that the days of the day are numbered from 0 to 6, week = 0, Monday = 1, second = 2 too.

Otrimannya misyatsya:

To reject the threading month of the threading rock, pick the getMonth () method. Before that, as I said, name the methods in an intuitive and intelligent way!

var month = date.getMonth (); console.log (month); //one

* Beast respect, yak, yak and dnі nіtіzhnya, mіsyatsі rotate in the numeric format from 0 to 11; s_chen = 0, luthium = 1, birch = 2 meager.

Rejection of the day of the month:

Then you can cut off the day of the month, the GetDate () method is also possible.

var dayOfMonth = date.getDate (); console.log (dayOfMonth); // 2

* The GetDate () method rotated the days of the month, numbered from 1 to 31; number 1 on the first day of the month.

Rejecting the hour:

To remove the flowing hour, the GetTime () method is used.

var time = date.getTime (); console.log (time); // 1454461242058

Viglyadaє ... wonderful ... So tse take? Take the number of milliseconds you have passed since 1 September 1970 00:00:00 UTC. Is it a little bit of rock?

1454461242058 ms / 31540000000 ms at pik = close to 46 rockiv
1970 + 46 rock_v = 2016 рік

Let's change it, let's get over it:

var year = date.getFullYear (); console.log (year) // 2016

So tse wono! Mi tilki scho viveli for the additional JavaScript new date, I will pinpoint the date that hour.

Get ready for the project - please, dyakuyu!
Read also
If the apple stops signing up for ios 10 If the apple stops signing up for ios 10 A look at the electronic vag Xiaomi Mi Smart Scale: a stitch behind you A look at the electronic vag Xiaomi Mi Smart Scale: a stitch behind you This is also a YouTube playlist and everything about new This is also a YouTube playlist and everything about new