1 创建一个新的日期对象,如果不带参数,则对象自动获得当前的日期和时间
var d = new Date()
2 如果需要指定特定的日期,则可以通过Date.parse() 或者 Date().UTC(),返回时间戳作为 new Date()的参数
Date.parse() 用法:
var time = Date.parse('2015/05/20');
var newDate = new Date(time);//Wed May 20 2015 00:00:00 GMT+0800 (中国标准时间)
//转换为格林威治时间
newDate.toUTCString(); //Tue, 19 May 2015 16:00:00 GMT
也可以直接 new Date('2015/05/20') 指定日期,new Date() 构造函数会自动调用 Date.parse()静态方法。
Date.UTC()
Date.UTC()的参数分别是年,月(从0到11),日(1-31),时(0-23),分(0-59),秒(0-59),毫秒(0-999),最少参数2个,即应该包含年月,其他不填的默认为0。
如果要创建的时间为中国标准时间的2015年5月20日,则代码应表示为
var myDate = new Date(Date.UTC(2015,5,19,16,0,0)) //Sat Jun 20 2015 00:00:00 GMT+0800 //格林威治时间 myDate.toUTCString() // Fri, 19 Jun 2015 16:00:00 GMT
其他:
var d = new Date(); //年 d.getFullYear() //月 d.getMonth() //日 d.getDate()
下面我们通过具体的示例来看看
Date.isValiDate = function(dateStr, formatStr)
{
if(!dateStr){
return false;
}
if(!formatStr){
formatStr = "yyyy-MM-dd";//默认格式:yyyy-MM-dd
}
if(dateStr.length!=formatStr.length){
return false;
}else{
if(formatStr=="yyyy-MM-dd"||formatStr=="YYYY-MM-DD"){
var r1=/^(((((([02468][048])|([13579][26]))(00))|(d{2}(([02468][48])|([13579][26]))))-((((0[13578])|(1[02]))-(([0-2][0-9])|(3[01])))|(((0[469])|(11))-(([0-2][0-9])|(30)))|(02-([0-2][0-9]))))|(d{2}(([02468][1235679])|([13579][01345789]))-((((0[13578])|(1[02]))-(([0-2][0-9])|(3[01])))|(((0[469])|(11))-(([0-2][0-9])|(30)))|(02-(([0-1][0-9])|(2[0-8]))))))$/;
return r1.test(dateStr);
}else if(formatStr=="yyyy/MM/dd"||formatStr=="YYYY/MM/DD"){
var r2=/^(((((([02468][048])|([13579][26]))(00))|(d{2}(([02468][48])|([13579][26]))))/((((0[13578])|(1[02]))/(([0-2][0-9])|(3[01])))|(((0[469])|(11))/(([0-2][0-9])|(30)))|(02/([0-2][0-9]))))|(d{2}(([02468][1235679])|([13579][01345789]))/((((0[13578])|(1[02]))/(([0-2][0-9])|(3[01])))|(((0[469])|(11))/(([0-2][0-9])|(30)))|(02/(([0-1][0-9])|(2[0-8]))))))$/;
return r2.test(dateStr);
}else if(formatStr=="MM-dd-yyyy"||formatStr=="MM-DD-YYYY"){
var r3=/^((((((0[13578])|(1[02]))-(([0-2][0-9])|(3[01])))|(((0[469])|(11))-(([0-2][0-9])|(30)))|(02-([0-2][0-9])))-(((([02468][048])|([13579][26]))(00))|(d{2}(([02468][48])|([13579][26])))))|(((((0[13578])|(1[02]))-(([0-2][0-9])|(3[01])))|(((0[469])|(11))-(([0-2][0-9])|(30)))|(02-(([0-1][0-9])|(2[0-8])))))-d{2}(([02468][1235679])|([13579][01345789])))$/;
return r3.test(dateStr);
}else if(formatStr=="MM/dd/yyyy"||formatStr=="MM/DD/YYYY"){
var r4=/^((((((0[13578])|(1[02]))/(([0-2][0-9])|(3[01])))|(((0[469])|(11))/(([0-2][0-9])|(30)))|(02/([0-2][0-9])))/(((([02468][048])|([13579][26]))(00))|(d{2}(([02468][48])|([13579][26])))))|(((((0[13578])|(1[02]))/(([0-2][0-9])|(3[01])))|(((0[469])|(11))/(([0-2][0-9])|(30)))|(02/(([0-1][0-9])|(2[0-8])))))/d{2}(([02468][1235679])|([13579][01345789])))$/;
return r4.test(dateStr);
}else{
alert("日期格式不正确!");
return false;
}
}
return false;
}
Date.isValiTime = function(timeStr, formatStr)
{
if(!timeStr){
return false;
}
if(!formatStr){
formatStr = "hh:mm:ss";//默认格式:hh:mm:ss
}
if(timeStr.length!=formatStr.length){
return false;
}else{
if(formatStr=="hh:mm:ss"){
var r1=/^(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])$/;
return r1.test(timeStr);
}else if(formatStr=="hh-mm-ss"){
var r2=/^(([0-1][0-9])|(2[0-3]))-([0-5][0-9])-([0-5][0-9])$/;
return r2.test(timeStr);
}else if(formatStr=="hh/mm/ss"){
var r3=/^(([0-1][0-9])|(2[0-3]))/([0-5][0-9])/([0-5][0-9])$/;
return r3.test(timeStr);
}else{
alert("时间格式不正确!");
return false;
}
}
return false;
}
Date.isValiDateTime = function(dateTimeStr)
{
var dateTimeReg=/^(((((([02468][048])|([13579][26]))(00))|(d{2}(([02468][48])|([13579][26]))))-((((0[13578])|(1[02]))-(([0-2][0-9])|(3[01])))|(((0[469])|(11))-(([0-2][0-9])|(30)))|(02-([0-2][0-9]))))|(d{2}(([02468][1235679])|([13579][01345789]))-((((0[13578])|(1[02]))-(([0-2][0-9])|(3[01])))|(((0[469])|(11))-(([0-2][0-9])|(30)))|(02-(([0-1][0-9])|(2[0-8]))))))(s{1}(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9]))?$/
return dateTimeReg.test(dateTimeStr);
}
Date.prototype.isLeapYear = function()
{
return (this.getYear()%4==0&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
}
Date.prototype.format = function(formatStr)
{
var str = formatStr;
if(!formatStr){
str = "yyyy-MM-dd hh:mm:ss";//默认格式
}
var Week = ['日','一','二','三','四','五','六'];
str=str.replace(/yyyy|YYYY/,this.getFullYear());
str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
str=str.replace(/MM/,this.getMonth()>=9?(parseInt(this.getMonth())+1).toString():'0' + (parseInt(this.getMonth())+1));
str=str.replace(/M/g,(parseInt(this.getMonth())+1));
str=str.replace(/w|W/g,Week[this.getDay()]);
str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
str=str.replace(/d|D/g,this.getDate());
str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
str=str.replace(/h|H/g,this.getHours());
str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
str=str.replace(/m/g,this.getMinutes());
str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
str=str.replace(/s|S/g,this.getSeconds());
str=str.replace(/iii/g,this.getMilliseconds()<10?'00'+this.getMilliseconds():(this.getMilliseconds()<100?'0'+this.getMilliseconds():this.getMilliseconds()));
return str;
}
Date.stringToDate = function(dateStr)
{
if(!dateStr){
alert("字符串无法解析为日期");
return null;
}else{
if(Date.isValiDate(dateStr,"yyyy/MM/dd")||Date.isValiDate(dateStr,"MM/dd/yyyy")){
return new Date(Date.parse(dateStr));
}else{
if((!-[1,])){//IE
if(Date.isValiDate(dateStr,"MM-dd-yyyy")){
return new Date(Date.parse(dateStr));
}else{
alert("字符串无法解析为日期");
return null;
}
}else{//非IE
if(Date.isValiDate(dateStr,"yyyy-MM-dd")){
return new Date(Date.parse(dateStr));
}else{
alert("字符串无法解析为日期");
return null;
}
}
}
}
return null;
}
Date.daysBetween = function(dateOne,dateTwo)
{
if((dateOne instanceof Date)==false||(dateTwo instanceof Date)==false){
return 0;
}else{
return Math.abs(Math.floor((dateOne.getTime()-dateTwo.getTime())/1000/60/60/24));
}
}
Date.prototype.dateAdd = function(num, field)
{
if((!num)||isNaN(num)||parseInt(num)==0){
return this;
}
if(!field){
field = "d";
}
switch(field){
case 'Y':
case 'y':return new Date((this.getFullYear()+num), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());break;
case 'Q':
case 'q':return new Date(this.getFullYear(), (this.getMonth()+num*3), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());break;
case 'M':return new Date(this.getFullYear(), this.getMonth()+num, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());break;
case 'W':
case 'w':return new Date(Date.parse(this) + ((86400000 * 7) * num));break;
case 'D':
case 'd':return new Date(Date.parse(this) + (86400000 * num));break;
case 'H':
case 'h':return new Date(Date.parse(this) + (3600000 * num));break;
case 'm':return new Date(Date.parse(this) + (60000 * num));break;
case 'S':
case 's':return new Date(Date.parse(this) + (1000 * num));break;
default: return this;
}
return this;
}
Date.prototype.dateDiff = function(dtEnd, field)
{
var dtStart = this;
if((dtEnd instanceof Date)==false){
return 0;
}else{
if(!field){
field = "d";
}
switch(field){
case 'Y':
case 'y':return dtEnd.getFullYear() - dtStart.getFullYear();break;
case 'M':return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);break;
case 'W':
case 'w':return parseInt((dtEnd - dtStart) / (86400000 * 7));break;
case 'D':
case 'd':return parseInt((dtEnd - dtStart) / 86400000);break;
case 'H':
case 'h':return parseInt((dtEnd - dtStart) / 3600000);break;
case 'm':return parseInt((dtEnd - dtStart) / 60000);break;
case 'S':
case 's':return parseInt((dtEnd - dtStart) / 1000);break;
default: return 0;
}
return 0;
}
}
Date.prototype.toArray = function()
{
var myArray = new Array();
myArray[0] = this.getFullYear();
myArray[1] = this.getMonth();
myArray[2] = this.getDate();
myArray[3] = this.getHours();
myArray[4] = this.getMinutes();
myArray[5] = this.getSeconds();
return myArray;
}
Date.prototype.datePart = function(field)
{
if(!field){
field = "d";
}
var Week = ['日','一','二','三','四','五','六'];
switch (field){
case 'Y' :
case 'y' :return this.getFullYear();break;
case 'M' :return (this.getMonth()+1);break;
case 'W' :
case 'w' :return Week[this.getDay()];break;
case 'D' :
case 'd' :return this.getDate();break;
case 'H' :
case 'h' :return this.getHours();break;
case 'm' :return this.getMinutes();break;
case 's' :return this.getSeconds();break;
default:return this.getDate();
}
return this.getDate();
}



