A detailed description of the JavaScript Date object (in date)

Source: Internet
Author: User
Tags local time


Date Object


The Date object is used to process dates and times.


To create the syntax for a Date object:
var mydate=new Date ();


Note: The Date object automatically saves the current date and time as its initial value.



1. Date Initialization settings


 new Date("m  d,y h:m:s");
new  Date("m d,y");
new  Date(y,m,d,h,m,s);
new Date(y,m,d);
new Date(ms);
Example:
new Date("January 12,2006 22:19:35");
new Date("January 12,2006");
new Date(2006,0,12,22,19,35);
new Date(2006,0,12);
new Date(1137075575000); 


2. Date Object method


Method description
Date() returns the date and time of the day.
Getdate() returns a day of the month (1 to 31) from the date object.
Getday() returns a day of the week (0 to 6) from the date object.
Getmonth() returns the month (0 to 11) from the date object.
Getfullyear() returns the year in four digits from the date object.
Getyear() use the getfullyear() method instead.
Gethours() returns the hour (0 to 23) of the date object.
Getminutes() returns the minute (0 to 59) of the date object.
Getseconds() returns the number of seconds (0 to 59) of the date object.
Getmilliseconds() returns the milliseconds (0 to 999) of the date object.
Gettime() returns the number of milliseconds since January 1, 1970.
Gettimezoneoffset() returns the minute difference between local time and Greenwich mean time (GMT).
Getutcdate() returns the day of the month (1 to 31) from the date object based on world time.
Getutcday() returns one day of the week (0 to 6) from the date object based on world time.
Getutcmonth() returns the month (0 to 11) from the date object based on world time.
Getutcfullyear() returns a four digit year from the date object based on world time.
Getuthours() returns the hour (0 to 23) of the date object based on world time.
Getutcminutes() returns the minute (0 to 59) of the date object based on world time.
Getutcseconds() returns the seconds (0 to 59) of the date object based on world time.
Getutcmilliseconds() returns the milliseconds (0 to 999) of the date object based on world time.
Parse() returns the number of milliseconds from midnight on January 1, 1970 to the specified date (string).
Setdate() sets the day of the month in the date object (1 to 31).
Setmonth() sets the month (0 to 11) in the date object.
Setfullyear() sets the year (four digits) in the date object.
Setyear() use the setfullyear() method instead.
Sethours() sets the hour (0 to 23) in the date object.
Setminutes() sets the minutes (0 to 59) in the date object.
Setseconds() sets the seconds (0 to 59) in the date object.
Setmilliseconds() sets the milliseconds (0 to 999) in the date object.
Settime() sets the date object in milliseconds.
Setutcdate() sets the day of the month in the date object based on world time (1-31).
Setutcmonth () sets the month (0 to 11) in the date object based on world time.
Setutcfullyear() sets the year (four digits) in the date object based on world time.
Setuthours () sets the hours (0 to 23) in the date object based on world time.
Setutcminutes() sets the minutes (0 to 59) in the date object based on world time.
Setutcseconds() sets the seconds (0 to 59) in the date object based on world time.
Setutcmilliseconds() sets the milliseconds (0 to 999) in the date object based on world time.
ToSource () returns the source code of the object.
Tostring() converts the date object to a string.
Totimestring() converts the time part of the date object to a string.
Todatestring() converts the date part of the date object to a string.
Togmtstring() use the toutcstring() method instead.
Toutcstring () converts a date object to a string based on world time.
Tolocalestring() converts a date object to a string based on the local time format.
Tolocaletimestring() converts the time part of the date object to a string based on the local time format.
Tolocaledatestring() converts the date part of the date object to a string based on the local time format.
UTC () returns the number of milliseconds from January 1, 1970 to the specified date based on universal time.
Valueof() returns the original value of the date object. 


3. Date formatting


//Extension of date, converting date to string of specified format
//1-2 placeholders can be used for month (m), day (d), hour (H), minute (m), second (s), quarter (q),
//Year (y) can use 1-4 placeholders, MS (s) can only use 1 placeholder (is 1-3 digits)
/ / example:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function(fmt)
{ //author: meizz
Var o = {
"M +": this. Getmonth() + 1, / / month
"D +": this. Getdate(), / / day
"H +": this. Gethours(), / / hours
"M +": this. Getminutes(), / / points
"S +": this. Getseconds(), / / second
"Q +": math. Floor ((this. Getmonth() + 3) / 3), / / quarter
"S": this. Getmilliseconds() / / MS
}
if(/(y+)/.test(fmt))  
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
for(var k in o)  
if(new RegExp("("+ k +")").test(fmt))   
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));  
return fmt;  
}
Call method:
[javascript] view plaincopy
var time1 = new Date().format("yyyy-MM-dd HH:mm:ss");
var time2 = new Date().format("yyyy-MM-dd");
Mode two:
* *
*Extension of date, converting date to string of specified format
*1-2 placeholders can be used for month (m), day (d), 12 hours (H), 24 hours (H), minute (m), second (s), week (E), quarter (q)
*Year (y) can use 1-4 placeholders, MS (s) can only use 1 placeholder (is 1-3 digits)
* eg:
* (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
*(New date()). Pattern ("yyyy MM DD e HH: mm: SS") = = > 2009-03-10 two 20:09:04
*(New date()). Pattern ("yyyy MM DD EE HH: mm: SS") = > 2009-03-10 Tuesday 08:09:04
*(New date()). Pattern ("yyyy MM DD EEE HH: mm: SS") = > 2009-03-10 Tuesday 08:09:04
* (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 
* /
Date.prototype.pattern=function(fmt) {
var o = {    
"M +": this. Getmonth() + 1, / / month
"D +": this. Getdate(), / / day
"H +": this. Gethours()% 12 = = 0? 12: this. Gethours()% 12, / / hour
"H +": this. Gethours(), / / hours
"M +": this. Getminutes(), / / points
"S +": this. Getseconds(), / / second
"Q +": math. Floor ((this. Getmonth() + 3) / 3), / / quarter
"S": this. Getmilliseconds() / / MS
}
var week = {    
"0" : "/u65e5",    
"1" : "/u4e00",    
"2" : "/u4e8c",    
"3" : "/u4e09",    
"4" : "/u56db",    
"5" : "/u4e94",    
"6" : "/u516d"    
}
if(/(y+)/.test(fmt)){    
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));        
}
if(/(E+)/.test(fmt)){    
fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);        
}
for(var k in o){    
if(new RegExp("("+ k +")").test(fmt)){        
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));           
}
}
return fmt;    
}
//Call mode
var date = new Date();
window.alert(date.pattern("yyyy-MM-dd hh:mm:ss"));
Mode three:
Date.prototype.format = function(mask) {
var d = this;    
var zeroize = function (value, length) {    
if (!length) length = 2;        
value = String(value);        
for (var i = 0, zeros = ‘‘; i < (length - value.length); i++) {        
zeros += ‘0‘;        
}
return zeros + value;        
}
return mask.replace(/"[^"]*"|‘[^‘]*‘|/b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])/1?|[lLZ])/b/g, function($0) {    
switch($0) {        
case ‘d‘:   return d.getDate();        
case ‘dd‘:  return zeroize(d.getDate());        
case ‘ddd‘: return [‘Sun‘,‘Mon‘,‘Tue‘,‘Wed‘,‘Thr‘,‘Fri‘,‘Sat‘][d.getDay()];        
case ‘dddd‘:    return [‘Sunday‘,‘Monday‘,‘Tuesday‘,‘Wednesday‘,‘Thursday‘,‘Friday‘,‘Saturday‘][d.getDay()];        
case ‘M‘:   return d.getMonth() + 1;        
case ‘MM‘:  return zeroize(d.getMonth() + 1);        
case ‘MMM‘: return [‘Jan‘,‘Feb‘,‘Mar‘,‘Apr‘,‘May‘,‘Jun‘,‘Jul‘,‘Aug‘,‘Sep‘,‘Oct‘,‘Nov‘,‘Dec‘][d.getMonth()];        
case ‘MMMM‘:return [‘January‘,‘February‘,‘March‘,‘Ap
ril‘,‘May‘,‘June‘,‘July‘,‘August‘,‘September‘,‘October‘,‘November‘,‘December‘][d.getMonth()];
case ‘yy‘:  return String(d.getFullYear()).substr(2);        
case ‘yyyy‘:    return d.getFullYear();        
case ‘h‘:   return d.getHours() % 12 || 12;        
case ‘hh‘:  return zeroize(d.getHours() % 12 || 12);        
case ‘H‘:   return d.getHours();        
case ‘HH‘:  return zeroize(d.getHours());        
case ‘m‘:   return d.getMinutes();        
case ‘mm‘:  return zeroize(d.getMinutes());        
case ‘s‘:   return d.getSeconds();        
case ‘ss‘:  return zeroize(d.getSeconds());        
case ‘l‘:   return zeroize(d.getMilliseconds(), 3);        
case ‘L‘:   var m = d.getMilliseconds();        
if (m > 99) m = Math.round(m / 10);        
return zeroize(m);        
case ‘tt‘:  return d.getHours() < 12 ? ‘am‘ : ‘pm‘;        
case ‘TT‘:  return d.getHours() < 12 ? ‘AM‘ : ‘PM‘;        
case ‘Z‘:   return d.toUTCString().match(/[A-Z]+$/);        
// Return quoted strings with the surrounding quotes removed        
default:    return $0.substr(1, $0.length - 2);        
}
};
} 





A detailed description of the JavaScript Date object (in date)


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.