Home  >  Article  >  Web Front-end  >  JavaScript Advanced Programming Reading Notes No. 10 Local Object Date_Javascript Skills

JavaScript Advanced Programming Reading Notes No. 10 Local Object Date_Javascript Skills

WBOY
WBOYOriginal
2016-05-16 17:55:471067browse

Create
var d=new Date();
Note that in JavaScript the month value is from 0 to 11 (0 means January).

Set date and time values
There are two ways to set date and time values:

1. Only declare the number of milliseconds from 12:00 a.m. on January 1, 1970

a. Directly use the number of milliseconds from 12 a.m. on January 1, 1970

var d=new Date(0);
b. parse method:

parse The method accepts a string as a parameter, converts the string into a date value, and returns the number of milliseconds.

For example, create a Date object for February 27, 2012:

var d=new Date(Date.parse("Feb 27,2012"));
If passed to parse The string of the method cannot be converted into a date. The function returns NaN

c. UTC method:

The UTC method also returns the millisecond representation of the date, but the parameters are year, month, day, hour, Minutes, seconds, milliseconds, year and month are required, others are optional.

For example, create a Date object for February 27, 2012:

var d=new Date(Date.UTC(2012,1,27));
2. Directly declare UTC Parameters accepted by the method

var d=new Date(2012,1,27);
 The parameter rules are the same as the UTC method.

Date class method
Date class method is as follows (from: http://www.jb51.net/w3school/js/jsref_obj_date.htm):

Method Description FF IE
Date() Returns the date and time of the current day. 1 3
getDate() Returns the day of the month (1 ~ 31) from the Date object. 1 3
getDay() Returns the day of the week (0 ~ 6) from a Date object. 1 3
getMonth() Returns the month (0 ~ 11) from the Date object. 1 3
getFullYear() Returns the year as a four-digit number from a Date object. 1 4
getYear() Please use getFullYear() method instead. 1 3
getHours() Returns the hour of the Date object (0 ~ 23). 1 3
getMinutes() Returns the minute (0 ~ 59) of the Date object. 1 3
getSeconds() Returns the number of seconds in a Date object (0 ~ 59). 1 3
getMilliseconds() Returns the millisecond (0 ~ 999) of the Date object. 1 4
getTime() Returns the number of milliseconds since January 1, 1970. 1 3
getTimezoneOffset() Returns the difference in minutes between local time and Greenwich Mean Time (GMT). 1 3
getUTCDate() Returns the day of the month (1 ~ 31) from a Date object based on universal time. 1 4
getUTCDay() Returns the day of the week (0 ~ 6) from a Date object based on universal time. 1 4
getUTCMonth() Returns the month (0 ~ 11) from the Date object according to universal time. 1 4
getUTCFulYear() Returns the four-digit year from a Date object based on universal time. 1 4
getUTCHours() Returns the hour of the Date object according to universal time (0 ~ 23). 1 4
getUTCMinutes() Returns the minute of the Date object according to universal time (0 ~ 59). 1 4
getUTCSeconds() Returns the seconds (0 ~ 59) of the Date object according to universal time. 1 4
getUTCMilliseconds() Returns milliseconds (0 ~ 999) of a Date object according to universal time. 1 4
parse() Returns the number of milliseconds from midnight on January 1, 1970 to the specified date (string). 1 3
setDate() Set the day of the month (1 ~ 31) in the Date object. 1 3
setMonth() Set the month (0 ~ 11) in the Date object. 1 3
setFullYear() Set the year (four digits) in the Date object. 1 4
setYear() Please use the setFullYear() method instead. 1 3
setHours() Set the hour (0 ~ 23) in the Date object. 1 3
setMinutes() Set the minute (0 ~ 59) in the Date object. 1 3
setSeconds() Sets the seconds in the Date object (0 ~ 59). 1 3
setMilliseconds() Set the milliseconds (0 ~ 999) in the Date object. 1 4
setTime() Sets the Date object in milliseconds. 1 3
setUTCDate() Set the day of the month in the Date object (1 ~ 31) according to universal time. 1 4
setUTCMonth() Sets the month (0 ~ 11) in the Date object according to universal time. 1 4
setUTCFulYear() Sets the year (four digits) in the Date object according to universal time. 1 4
setUTCHours() Sets the hour in the Date object according to universal time (0 ~ 23). 1 4
setUTCMinutes() Set the minute in the Date object according to universal time (0 ~ 59). 1 4
setUTCSeconds() Sets the seconds in the Date object (0 ~ 59) according to universal time. 1 4
setUTCMilliseconds() Sets the milliseconds in the Date object according to universal time (0 ~ 999). 1 4
toSource() Returns the source code of this object. 1 -
toString() Convert Date object to string. 1 4
toTimeString() Convert the time part of the Date object to a string. 1 4
toDateString() Convert the date part of the Date object to a string. 1 4
toGMTString() Please use toUTCString() method instead. 1 3
toUTCString() Convert Date object to string according to universal time. 1 4
toLocaleString() Convert Date object to string according to local time format. 1 3
toLocaleTimeString() Convert the time part of the Date object into a string according to the local time format. 1 3
toLocaleDateString() Convert the date part of the Date object into a string according to the local time format. 1 3
UTC() Returns the number of milliseconds between January 1, 1997 and the specified date according to universal time. 1 3
valueOf() Returns the original value of the Date object. 1 4

Share a date formatting method
Share a date formatting method here, the usage method is the same as DateTime in C# The ToString method is similar:
Copy code The code is as follows:

Date.prototype.toString=function( format){
var time={};
time.Year=this.getFullYear();
time.TYear=("" time.Year).substr(2);
time. Month=this.getMonth() 1;
time.TMonth=time.Month<10?"0" time.Month:time.Month;
time.Day=this.getDate();
time .TDay=time.Day<10?"0" time.Day:time.Day;
time.Hour=this.getHours();
time.THour=time.Hour<10?"0" time .Hour:time.Hour;
time.hour=time.Hour<13?time.Hour:time.Hour-12;
time.Thour=time.hour<10?"0" time.hour: time.hour;
time.Minute=this.getMinutes();
time.TMinute=time.Minute<10?"0" time.Minute:time.Minute;
time.Second=this. getSeconds();
time.TSecond=time.Second<10?"0" time.Second:time.Second;
time.Millisecond=this.getMilliseconds();

var oNumber= time.Millisecond/1000;

if(format!=undefined && format.replace(/s/g,"").length>0){
format=format
.replace(/ yyyy/ig,time.Year)
.replace(/yyy/ig,time.Year)
.replace(/yy/ig,time.TYear)
.replace(/y/ig,time .TYear)
.replace(/MM/g,time.TMonth)
.replace(/M/g,time.Month)
.replace(/dd/ig,time.TDay)
.replace(/d/ig,time.Day)
.replace(/HH/g,time.THour)
.replace(/H/g,time.Hour)
.replace(/ hh/g,time.Thour)
.replace(/h/g,time.hour)
.replace(/mm/g,time.TMinute)
.replace(/m/g,time .Minute)
.replace(/ss/ig,time.TSecond)
.replace(/s/ig,time.Second)
.replace(/fff/ig,time.Millisecond)
.replace(/ff/ig,oNumber.toFixed(2)*100)
.replace(/f/ig,oNumber.toFixed(1)*10);
}
else{
format=time.Year "-" time.Month "-" time.Day " " time.Hour ":" time.Minute ":" time.Second;
}
return format;
}

var d=new Date();
console.log(d.toString()); //2011-12-29 11:29:43
console.log(d.toString ("")); //2011-12-29 11:29:43
console.log(d.toString("yyyy-MM-dd")); //2011-12-29
console .log(d.toString("HH:mm:ss")); //11:29:43
console.log(d.toString("yyyy-MM-dd HH:mm:ss")); //2011-12-29 11:29:43
console.log(d.toString("MM, dd, yyyy, HH:mm:ss")); //December 29, 2011 11:29 :43
console.log(d.toString("yyyy-MM-dd HH:mm:ss fff")); //2011-12-29 11:29:43 862
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