Home  >  Article  >  Web Front-end  >  Detailed explanation of new date() parameters in js

Detailed explanation of new date() parameters in js

小云云
小云云Original
2018-03-19 17:53:1618843browse

This article mainly shares with you the detailed explanation of the new date() parameter in js. I hope it can help you. Commonly used parameter initialization methods for JS Date objects:

1. Initialize date objects with integers
var date1 = new Date(2017,06,06); console.log(date1 ); // Thu Jul 06 2017 00:00:00 GMT+0800 (China Standard Time)
var date1 = new Date(2017,1,1); console.log(date1); // Wed Feb 01 2017 00:00:00 GMT+0800 (China Standard Time)
var date1 = new Date(2017,01-2,01); console.log(date1); // Thu Dec 01 2016 00:00:00 GMT +0800 (China Standard Time)
var date1 =new Date(2017,06,06,06,06,06); console.log(date1); // Thu Jul 06 2017 06:06:06 GMT+0800 (China Standard Time)
Description: new Date(year, month, date, hrs, min, sec) Create a date object according to the given parameters

2. Use characters String initialization date object
var date2 = new Date(“2017/06/06”); console.log(date2); // Tue Jun 06 2017 00:00:00 GMT+0800 (China Standard Time)
var date2 = new Date(“2017-08-08”); console.log(date2); // Tue Aug 08 2017 08:00:00 GMT+0800 (China Standard Time)
var date2 = new Date ("2017-9-9"); console.log(date2); // Sat Sep 09 2017 00:00:00 GMT+0800 (China Standard Time)
Description: If the string mode does not support the dash mode, perform string replacement:
var strTime=”2011-04-16”;
var date2= new Date(Date.parse(strTime.replace (/-/g, “/”))); // /-/g is a regular expression (RegExp) object, indicating global replacement - to /.

3. Initialize the date object with millisecond timestamp
var timestamp=new Date().getTime(); console.log( new Date(timestamp) ); //Tue Jun 06 2017 11:06 :59 GMT+0800 (China Standard Time)
var date3 = new Date( timestamp - 1 * 60 * 60 * 1000); console.log(date3); // Tue Jun 06 2017 10:06:59 GMT+ 0800 (China Standard Time)
Explanation: The timestamp refers to 00:00:00 Greenwich Time on January 1, 1970 (08:00 on January 1, 1970 Beijing time 00 seconds) to now. A timestamp uniquely identifies a moment in time.

Call new Date() separately in js, for example document.write(new Date());

The displayed result is: Mar 31 10: 10:43 UTC+0800 2012 The time in this format

==

cannot be obtained in the safari browser when tested. Short-term patterns such as new Date(“2017-08-08”) are not supported. Just change it to a slash.

The above is the detailed content of Detailed explanation of new date() parameters in js. For more information, please follow other related articles on the PHP Chinese website!

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