Date and time in JavaScript
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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
