Date date object

Date date object

In JavaScript, the Date object is used to represent date and time.

To get the current system time, 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 obtained by the browser from the local operating system, so it may not be accurate because the user can set the current time for any value.

If you want to create a Date object that specifies the date and time, 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 cheating place, that is, the month range in JavaScript is represented by an integer, which is 0~ 11, 0 represents January, 1 represents February..., so to represent June, we pass in 5! This was definitely something that the designers of JavaScript had a brainwave at the time, but it's impossible to fix it now.

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 a Date object, but a timestamp. However, you can easily convert it to a Date with a timestamp:

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

Time Zone

The time represented by the Date object is always displayed according to the time zone of the browser. , but we can display both local time and 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 to perform time zone conversion in JavaScript? In fact, as long as we pass a timestamp of type number, we don't need to care about time zone conversion. Any browser can correctly convert a timestamp to local time.

What is a timestamp? The timestamp is a self-increasing integer that represents the number of milliseconds since January 1, 1970, 0:00 GMT time zone, to the present. Assuming that the time of the computer where the browser is located is accurate, the timestamp numbers generated by computers in any time zone in the world will be the same at this moment. Therefore, the timestamp can accurately represent a moment and has nothing to do with the time zone.

So, we only need to pass the timestamp, or read the timestamp from the database, and then let JavaScript automatically convert it to local time.

To get the current timestamp, you can use:

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


Continuing Learning
||
无标题文档
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!