Liaoche JS Tutorial Note 8 Date Object Introduction and Handling

Source: Internet
Author: User

In JavaScript, Date objects are used to represent dates and times.

To get the current time of the system, use:

var now = new Date();now; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)now.getFullYear(); // 2015, 年份now.getMonth(); // 5, 月份,注意月份范围是0~11,5表示六月now.getDate(); // 24, 表示24号now.getDay(); // 3, 表示星期三now.getHours(); // 19, 24小时制now.getMinutes(); // 49, 分钟now.getSeconds(); // 22, 秒now.getMilliseconds(); // 875, 毫秒数now.getTime(); // 1435146562875, 以number形式表示的时间戳

Note that the current time is the time that the browser obtains from the native operating system, so it is not necessarily accurate because the user can set the current time to any value.

If you want to create an object that specifies a date and time Date , you can use:

var d = new Date(2015, 5, 19, 20, 15, 30, 123);d; // Fri Jun 19 2015 20:15:30 GMT+0800 (CST)

You may have observed a very, very great pit-daddy , that is, the JavaScript month range is represented by integers as 0~11, which means 0 January, which 1 means February ..., so to say June, we passed in 5 ! It was definitely the JavaScript designer who had a bit of a brain pumping, but now it's impossible to fix it.

The second way to create a specified date and time is to parse a string that conforms to the ISO 8601 format:

var d = Date.parse(‘2015-06-24T19:49:22.875+08:00‘);d; // 1435146562875

But it returns not Date an object, but a timestamp. But with a time stamp it is easy to convert it to a Date :

var d = new Date(1435146562875);d; // Wed Jun 24 2015 19:49:22 GMT+0800 (CST)
Time

DateThe time the object represents is always displayed in the browser's time zone, but we can display both the local time and the adjusted UTC time:

var d = new Date(1435146562875);d.toLocaleString(); // ‘2015/6/24 下午7:49:22‘,本地时间(北京时区+8:00),显示的字符串与操作系统设定的格式有关d.toUTCString(); // ‘Wed, 24 Jun 2015 11:49:22 GMT‘,UTC时间,与本地时间相差8小时

So how do you do time zone conversions in JavaScript? In fact, as long as we pass a timestamp of a number type, we don't care about the time zone conversion. Any browser can convert a timestamp to the local time correctly.

What's a time stamp? The timestamp is an auto-increment integer that represents the moment that the GMT time zone starts at zero January 1, 1970, and the current number of milliseconds. Assuming that the time of the browser's computer is accurate, the time stamp numbers in the world, regardless of the time zone, are the same at the moment, so the timestamp can represent exactly one moment and is independent of the time zone.

So, we just need to pass the timestamp, or read the timestamp out of the database and let JavaScript automatically convert to local time.

To get the current timestamp, you can use:

if (Date.now) {    alert(Date.now()); // 老版本IE没有now()方法} else {    alert(new Date().getTime());}

Liaoche JS Tutorial Note 8 Date Object Introduction and Handling

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: info-contact@alibabacloud.com 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.