Home  >  Article  >  Web Front-end  >  Date and time in JavaScript

Date and time in JavaScript

高洛峰
高洛峰Original
2016-11-28 11:41:001049browse

Foreword
The introduction of this article covers the following parts:

1. What does time standard refer to? What are the concepts, relationships and differences between UCT and GMT?


2. What are the standards for time expression?

3. Time processing in JS


Date and time standards
Not much to say about date standards - the AD era

is to commemorate the birth of the Jewish Jesus (Christ). The year he was born was 1 AD, and the year before his birth was called "n year BC"


Simply speaking, the standard of time is the starting point of time. As for years, for example, if we did not use the AD chronology, it would probably be very difficult for the world to communicate (the 54th year of the People’s Republic of China would probably confuse many foreigners)

There are actually quite a lot of time standards.

Atomic Time · Coordinated Universal Time (UTC) · Greenwich Mean Time (GMT) · Earth Time (TT) · Mechanical Time (DT)

In program development and even in daily life, UTC and GMT are more common .

UTC is the English abbreviation of Universal Time Coordinated. It is a second-based time scale stipulated and recommended by the International Radio Advisory Committee and maintained by the International Time Bureau (BIH). UTC is equivalent to the mean solar time on the prime meridian (that is, 0 degrees longitude), which was expressed in Greenwich Mean Time (GMT) in the past. Beijing time is 8 hours ahead of UTC time, based on 0000 UTC on January 1, 1999. For example, UTC time is zero o'clock, and Beijing time is 8 o'clock in the morning on January 1, 1999.

GMT (Greenwich Mean Time) is Greenwich Mean Time:
Since the earth’s orbit is not circular, and its speed changes with the distance between the earth and the sun, the apparent solar time lacks uniformity. The apparent length of the solar day is also affected by the inclination of the Earth's rotation axis relative to its orbital plane. To correct for these inhomogeneities, astronomers calculated the effects of the Earth's non-circular orbit and polar axis tilt toward the Sun. Mean solar time refers to the revised apparent solar time. The mean solar time on the Greenwich meridian is called Universal Time (UT0), also called Greenwich Mean Time (GMT).

In order to ensure that the difference between Coordinated Universal Time and Universal Time (UT1) will not exceed 0.9 seconds, positive or negative leap seconds will be added to Coordinated Universal Time when necessary. Therefore, there will be a number of whole seconds difference between Coordinated Universal Time and International Atomic Time (TAI). The International Center for Earth Rotation Affairs (IERS) in Paris is responsible for deciding when to add leap seconds.


The relationship between the two:

UTC = GMT +/- 0.9 s
Therefore, UTC needs to make "leap seconds" to control the difference between the two.


You can ignore all the above introductions. In a word, UTC and GMT are world standard time, and the difference between them is very small.

Date and time representation standard

The calculation standard for date and time is now available. But the expression can also vary widely.

2013/08/16

08/16/2013

13/08/16

. . . . .

So, the International Organization for Standardization ISO came out and formulated an 8601 series of "Data Storage and Exchange Forms·Information Exchange·Date and Time Representation Methods".

It is currently the third edition of ISO8601:2004 to replace the first edition of ISO8601:1988 and the second edition of ISO8601:2000.

The main parts are listed:

1. Date representation


The year is 4 digits, the month is 2 digits, and the day in the month is 2 digits, for example, date (August 16, 2013) It can be expressed as 2013-08-16, or 20130816.

2. Calendar week and day representation

You can use 2 digits to represent the calendar week of the year;

Each calendar week starts on Monday, and Sunday is the 7th day

3. Time representation


Hours, minutes and seconds are all represented by 2 digits. For UTC time, a capital letter Z is added at the end. Other time zones are represented by the actual time plus the time difference. For example, 2:30 minutes and 5 seconds of UTC time is expressed as 14:30:05Z or 143005Z, and the Beijing time at that time is expressed as 22:30:05+08:00 or 223005+0800, which can also be simplified to 223005+08.

4. Combined representation of date and time

When expressing together, add a capital letter T in front of the time. If you want to express 5:30 minutes and 8 seconds on May 3, 2004, Beijing time, you can write it as 2004-05 -03T17:30:08+08:00 or 20040503T173008+08.

What needs to be mentioned in particular is:

Sunday is the first day of the week in the West

It is the last day in China.

That is, the ISO standards are consistent with Chinese customs. However, many software, languages, etc. are currently developed abroad, and their own habits have continued over time.

Date and time in Javascript

Method Description FF IE
Date() Returns the date and time of the current day. 1 3
getDate() returns the day of the month (1 ~ 31) from the Date object. 1 3
getDay() Returns the day of the week (0 ~ 6) from the Date object. 1 3
getMonth() returns the month (0 ~ 11) from the Date object. 1 3
getFullYear() Returns the year as a four-digit number from a Date object. 1 4
getYear() Please use getFullYear() method instead. 1 3
getHours() returns the hours (0 ~ 23) of the Date object. 1 3
getMinutes() returns the minutes (0 ~ 59) of the Date object. 1 3
getSeconds() returns the seconds of the Date object (0 ~ 59). 1 3
getMilliseconds() returns the milliseconds (0 ~ 999) of the Date object. 1 4
getTime() Returns the number of milliseconds since January 1, 1970. 1 3
getTimezoneOffset() Returns the difference in minutes between local time and Greenwich Mean Time (GMT). 1 3
getUTCDate() Returns the day of the month (1 ~ 31) from a Date object based on universal time. 1 4
getUTCDay() Returns the day of the week (0 ~ 6) from a Date object based on universal time. 1 4
getUTCMonth() Returns the month (0 ~ 11) from the Date object according to universal time. 1 4
getUTCFulYear() Returns the four-digit year from a Date object based on universal time. 1 4
getUTCHours() Returns the hour (0 ~ 23) of the Date object according to universal time. 1 4
getUTCMinutes() Returns the minutes (0 ~ 59) of a Date object according to universal time. 1 4
getUTCSeconds() Returns the seconds (0 ~ 59) of a Date object according to universal time. 1 4
getUTCMilliseconds() returns the milliseconds (0 ~ 999) of the Date object according to universal time. 1 4
parse() Returns the number of milliseconds from midnight on January 1, 1970 to the specified date (string). 1 3
setDate() sets the day of the month (1 ~ 31) in the Date object. 1 3
setMonth() sets the month (0 ~ 11) in the Date object. 1 3
setFullYear() Sets the year (four digits) in the Date object. 1 4
setYear() Please use the setFullYear() method instead. 1 3
setHours() Sets the hours (0 ~ 23) in the Date object. 1 3
setMinutes() Sets the minutes (0 ~ 59) in the Date object. 1 3
setSeconds() Sets the seconds (0 ~ 59) in the Date object. 1 3
setMilliseconds() Sets the milliseconds (0 ~ 999) in the Date object. 1 4
setTime() Sets a Date object in milliseconds. 1 3
setUTCDate() Sets the day of the month in the Date object (1 ~ 31) according to universal time. 1 4
setUTCMonth() Sets the month (0 ~ 11) in the Date object according to universal time. 1 4
setUTCFulYear() Sets the year (four digits) in the Date object according to universal time. 1 4
setUTCHours() Sets the hour (0 ~ 23) in the Date object according to universal time. 1 4
setUTCMinutes() Sets the minutes in the Date object (0 ~ 59) according to universal time. 1 4
setUTCSeconds() Sets the seconds in the Date object (0 ~ 59) according to universal time. 1 4
setUTCMilliseconds() Sets the milliseconds in the Date object (0 ~ 999) according to universal time. 1 4
toSource() Returns the source code of this object. 1 -
toString() Converts Date object to string. 1 4
toTimeString() Converts the time part of the Date object to a string. 1 4
toDateString() Converts the date part of the Date object to a string. 1 4
toGMTString() Please use toUTCString() method instead. 1 3
toUTCString() Converts a Date object to a string according to universal time. 1 4
toLocaleString() Converts Date object to string according to local time format. 1 3
toLocaleTimeString() Converts the time part of the Date object to a string according to the local time format. 1 3
toLocaleDateString() Converts the date part of the Date object to a string according to the local time format. 1 3
UTC() Returns the number of milliseconds between January 1, 1997 and the specified date, according to Universal Time. 1 3
valueOf() returns the original value of the Date object.

You can also ignore all the above. What needs to be mentioned in particular is:

Javascript treats Sunday as the beginning of the week. You have to be careful when using the getDay() method.

getDay() returns a value between 0-6, 2013/08/18 is Sunday, the return value is 0

Add by oscar999--> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> New Document </TITLE> 
<META NAME="Author" CONTENT="oscar999"> 
<script> 
var date = new Date("2013/08/18"); 
alert(date.getDay()); 
</script> 
</HEAD> 
  
<BODY>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn