基本方法
var myDate = new Date();myDate.getYear(); //当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期
var mytime=myDate.toLocaleTimeString(); //获取当前时间
myDate.toLocaleString( ); //获取日期与时间
日期时间方法扩展
Date.prototype.isLeapYear 判断闰年
Date.prototype.isLeapYear = function() { return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0))); }
Date.prototype.Format 日期格式化
1 Date.prototype.format = function(format) 2 { 3 var o = { 4 "M+": this.getMonth() + 1, //month 5 "d+": this.getDate(), //day 6 "H+": this.getHours(), //hour 7 "m+": this.getMinutes(), //minute 8 "s+": this.getSeconds(), //second 9 "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 10 "S": this.getMilliseconds() //millisecond 11 } 12 if (/(y+)/.test(format)) format = format.replace(RegExp.$1, 13 (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 14 for (var k in o) if (new RegExp("(" + k + ")").test(format)) 15 format = format.replace(RegExp.$1, 16 RegExp.$1.length == 1 ? o[k] : 17 ("00" + o[k]).substr(("" + o[k]).length)); 18 return format; 19 } 20