ISO date format standard, browser-to-server-to-MySQL time zone

Source: Internet
Author: User
Tags gmt time zone cst time time zones local time

Time Zone Simple Comprehension


The link above is the time zone Wiki description, here is the part I remember:

GMT time zone is Greenwich Mean time, I interpret it as "real time"

UTC time zone is the "World standard Time" based on GMT, and its time and GMT are the same

CST can refer to the following time zones:

Central Australia Time (Australia)
Chubu Standard Time zone (North America)
Time in Beijing, China
Cuban STD time, Cuba standard Times, see North American Eastern Time zone

Where we are in the time zone background time cst=utc+8 hours, that is, the real time is 0 points, the background time is 8 points

iso_8601 Date Format Standard

The above is the date format standard wiki

The current UTC time is 2016-01-07t01:58z, where z is the time offset of the 4-bit number format and is not offset by default when not written.

Where the letter T represents the use of UTC time, the letter Z represents the time offset, the actual wording of the letter Z should be replaced by the offset, such as "2017-1-7t10:21+0800" or "2017-1-7t10:21-0800", the letter z is replaced by +0800 and 0800.

The new Date object is directly in the browser because we are in the utc+0800 time zone, so the time that the console prints to us is gmt+0800.

2016-01-07t00:00 for UTC time Zone January 7 0:0 (show utc+0800 time zone 8:0 in console)

2016-01-07t00:00 0800 represents the utc+0800 time zone January 7 0:0, 2016-01-07t00:00-0800 represents the UTC-0800 time zone January 7 0:0, displayed in the console respectively as follows

Using the date string new as a Date object, the input time string is 2016-1-7 10:21, with no ISO standard "T" letters, so the browser thinks we want to enter local time

HTML5 Input Tag DateTime property

The following question, I want to enter a time on the page, into the database, said so many time zones, then the user on the page input time should be which time zone, to the server, the DB should be in which time zone?

After testing

<input type= "datetime" is not supported on/> Chrome

<input type= "datetime-local"/> Chrome support

Google is said to be because the DateTime input box is entered in the local time zone, considering the time zone, Chrome does not support this input type, it will be downgraded to the text type

Datetime-local input type chrome is supported and gets the value in the format:


As mentioned above, this time is the standard UTC time, this time from the front to the background to the DB, there is no error.

But for users, the time he wanted to fill out the form was definitely the local time in his location, such as when I entered "2016-1-7 10:21", the time I really wanted to enter was "2016-1-7t10:21+0800", not "2016-1-7t10:21".

To support this situation, I need to convert "2016-1-7t10:21" to the local time that the user really wants "2016-1-7 10:21", so "2016-1-7 10:21". Replace ("T", ""), which actually represents the real time (UTC time) It's "2016-1-7t10:21+0800."

This is not a problem at the front end, but after uploading to the backend, such a non-ISO standard time is not carrying time zone information, the server will be received after the time to install the server in its time zone to process, after processing the real time represented by the user entered a different real time. So we have to convert the time in front of the ISO standard time format to the server, so that the server can understand the real time user input, another method, can also be expressed in milliseconds, to the backend, but this way is not very readable.

 //  1. Converts the literal time to local time 2. Convert local time to true GMT time incoming background  function   Getrealgmt (datetime) {Dateti        Me  =datetime.replace ("T", "");  var  temp=new   Date (datetime);  var  Realgmt=temp.gettime () +temp.gettimezoneoffset () *60000;  return  new  Date (REALGMT). Format    ("yyyy-mm-ddthh:mm" 
The conversion effect is as follows: MySQL time zone MySQL can be viewed by show variables like '%time_zone% '; command to view the time zone set by the database. Our CST time zone represents the time zone of the Chinese region, utc+0800 so the server writes the ISO date "2016-1-7t02:21" received from the front end to the DateTime field of MySQL, The datetime field of MySQL will convert the date according to its CST time zone, so the date displayed is "2016-1-7 10:21", the real time represented is "2016-1-7t10:21+0800"

ISO date format standard, browser-to-server-to-MySQL time zone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.