Home  >  Article  >  Web Front-end  >  JavaScript study notes (6) Date date type_basic knowledge

JavaScript study notes (6) Date date type_basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:52:301250browse

1. Create a date object

Copy code The code is as follows:

var now = new Date() ; //Get the current system date and time
var someDate = new Date(Date.parse("May 25,2012"));
var someDate = new Date("May 25,2012"); // Same as above
var someDate = new Date(Date.UTC(2010,0)); //GMT time at 0:00 am on January 1, 2010
var someDate = new Date(2010,0); / /Same as above
var someDate = new Date(Date.UTC(2010,4,5,17,55,55)); //GMT time 5:55:55 pm on May 2010, month and hour Taking 0 as the base, 0 represents January
var someDate = new Date(2010,4,5,17,55,55); //Same as above

Date.parse() and Date.UTC() returns the number of milliseconds of the corresponding date. The parameters of Date.UTC() are: year, month, day, hour, minute, second, and millisecond. There are at least two parameters: year and month. The month and hour start with 0 is the base
2. Comparison of dates
Copy code The code is as follows:

var date1 = new Date(2007,0,1);
var date2 = new Date(2007,1,1);
alert(date1>date2); //false
alert(date1

3. Date formatting method and method of obtaining and setting date
Copy code The code is as follows:

View Code
toDateString()
toTimeString()
toLocaleDateString() //such as January 1, 2007
toLocaleTimeString() //such as 13:55:55
toUTCString()
getTime() //Return the milliseconds of the date
setTime()
getFullYear() //Get the year, such as 2007
setFullYear()
getMonth() //Get the month, 0 means January
setMonth() //Set the month, 0 means January, increase the year if it exceeds 11
getDate() //Get the number of days in the date
setDate() //Set the number of days, increase the month if it exceeds 31
getDay() //Get the day of the week, 0 means Sunday, 6 means Saturday
getHours() //Get the hours, 0~23
setHours() //Set hours, 0~23
getMinutes() //Get minutes, 0~59
setMinutes() //Set minutes, 0~59
getSeconds() //Get seconds, 0~ 59
setSeconds() //Set seconds, 0~59
getMilliseconds() //Get milliseconds
setMilliseconds() //Set milliseconds
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