1创建一个新的日期对象,如果没有参数,对象自动获取当前日期和时间。


新日期()




2如果你需要指定一个特定的日期,可以作为新的参数返回日期时间戳()Date.parse()或()()UTC日期。

Date.parse()的用法:


VAR时间= date.parse('2015 / 05 / 20);
无功newdate =新的日期(时间); / /结婚可能202015 00:00:00 GMT + 0800(中国标准时间)
转换为格林尼治时间
newdate.toutcstring(); / /星期二,2015年5月19日16:00:00 GMT



你也可以直接新的日期('2015 / 05 / 20)的日期,和新的日期()构造函数自动调用date.parse()静态方法。

Date.UTC()

Date.UTC()的参数是:年,月(从0到11),天(1-31),时间(0-23),点(到59)、秒(到59),毫秒(0-999),和最小的参数2,即应包括年、和其他的默认值是0。

如果要为2015年5月20日的中国标准时间创建时间,则代码应表示为


var创建=新的日期(Date.UTC(2015,5,19,16,0,0)) / /坐军202015 00:00:00 GMT + 0800
格林尼治时间
MyDate.toUTCString(FRI) / 19军、2015 16:00:00 GMT



其他uff1a


新日期();
年/年
d.getfullyear()
月份
d.getmonth()
/天
d.getdate()



让我们来看看具体的例子。



*日期时间脚本库方法列表:
*(1)date.isvalidate:数据合法性验证
*(2)date.isvalitime:时间合法性验证
*(3)date.isvalidatetime:日期和时间的合法性验证
*(4)date.prototype.isleapyear:判断是否闰年
*(5)date.prototype.format:日期格式
*(6)date.stringtodate:字符串数据类型
*(7)date.daysbetween:在计算两个日期天数的差异
*(8)date.prototype.dateadd:日期计算,支持正负数
*(9)date.prototype.datediff:比较日期的差异:比较两个时期相同的领域和收益的差值
*(10)date.prototype.toarray:日期:根据阵列序号分为阵列分别为:日期
*(11)date.prototype.datepart:数据信息
* /


*数据合法性验证:确定是否符合规定的datastr formatstr日期格式
*实例:
*(1)警告(date.isvalidate('2008-02-29 ','yyyy-mm-dd ')); / /真的
*(2)警告(date.isvalidate('aaaa-58-29 ','yyyy-mm-dd ')); / /假
*中:需要日期字符串
* formatstr:可选的格式字符串,可选的格式是:(1)yyyy-mm-dd(默认格式)或为(2)年 / / DD或YYYY毫米/毫米/ DD(3)日期或mm-dd-yyyy(4)毫米/日/年或毫米/ DD /年。
* /
date.isvalidate =功能(中,formatstr)
{
如果(!DateStr){
返回false;
}
如果(!formatstr){
formatstr =yyyy-mm-dd; / /默认格式:yyyy-mm-dd
}
如果(datestr.length!= formatstr。长度){
返回false;
其他{ }
如果(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})))))) /;
返回r1.test(中);
} else if(formatstr = =yyyy /毫米/ DD| | formatstr = =yyyy /毫米/ 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})))))) /;
返回r2.test(中);
} else if(formatstr = =日期| | 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}));
返回r3.test(中);
} else if(formatstr = =毫米 / / DD yyyy| | formatstr = =MM / DD /年){
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}));
返回r4.test(中);
其他{ }
警报()日期格式不正确!;
返回false;
}
}
返回false;
}


*时间合法性验证:确定是否符合规定的timestr formatstr时间格式
*实例:
*(1)警告(date.isvalitime('23:59:59 ','hh:MM:SS)); / /真的
*(2)警告(date.isvalitime('24-68-89 ','hh:MM:SS)); / /假
* timestr:要求日期字符串
* formatstr:可选,格式字符串,可选的格式:(1)HH:毫米:SS(默认格式)(2)(3)SS HH毫米/毫米/ SS HH
* /
date.isvalitime =功能(timestr,formatstr)
{
如果(!timestr){
返回false;
}
如果(!formatstr){
formatstr =HH:MM:SS ; / /默认格式:HH:MM:SS
}
如果(timestr.length!= formatstr。长度){
返回false;
其他{ }
如果(formatstr = =HH:MM:SS){
Var(R1 = / ^({ 0 } { } |(0-9)2 { 0 })):({ 0 } { 0-9 }):({ 0 } { 0-9 }) /;
返回r1.test(timestr);
} else if(formatstr = =HH毫米不锈钢){
Var(R2 = / ^({ 0 } { } |(0-9)2 { 0 }))-({ 0 } { } -(0-9)} { } { 0 0-9) /;
返回r2.test(timestr);
} else if(formatstr = =HH /毫米/ SS){
Var(R3 = / ^({ 0 } { } |(0-9)2 { 0 }))/({ 0 } { 0-9 })/({ 0 } { 0-9 }) /;
返回r3.test(timestr);
其他{ }
警报时间格式不正确!;
返回false;
}
}
返回false;
}


*日期和时间合法性的验证
*格式:yyyy-mm-dd HH:毫米:SS
* /
date.isvalidatetime =功能(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}) /)
返回datetimereg.test(datetimestr);
}


*判断闰年:一般的规则是:四年,一个飞跃,一百年不闰,四百年飞跃。
* /
date.prototype.isleapyear =功能()
{
返回(this.getyear(% 4 = = 0)((这。getYear)= 0(100!)(这| |。getYear)(% 400 = = 0)));
}


*日期格式:
* formatstr:可选,格式字符串,默认格式:yyyy-mm-dd HH:毫米:SS
*同意以下格式:
*(1)YYYY / / /年YY YY代表年
*(2)毫米/米月
*(3)W/W周
*(4)DD / D / D / D日期
*(5);
*(6)毫米/分钟
*(7)SS秒/秒/秒
*(8)iii毫秒
* /
date.prototype.format =功能(formatstr)
{
var str = formatstr;
如果(!formatstr){
str =yyyy-mm-dd HH:毫米:SS ; / /默认格式
}
var一周=日,A,二,三,四,五,六};

STR = str.replace( /年| yyyy /,this.getfullyear());
STR = str.replace( / / | YY YY,(this.getyear()% 100)> 9(this.getyear()% 100)。ToString():0+(this.getyear()% 100)));

STR = str.replace( / /毫米,this.getmonth()> = 9(parseInt(this.getmonth())+ 1)。ToString():0+(parseInt(this.getmonth())+ 1)));
STR = str.replace( / / g,(((this.getmonth parseInt())+ 1)));

STR = str.replace( / W | W / g,周this.getday(){ });

STR = str.replace( / / DD | DD,this.getdate()> 9this.getdate()ToString()():0+ this.getdate()));
STR = str.replace( / D | D / G,this.getdate());

STR = str.replace( / / | HH HH,this.gethours()> 9this.gethours()ToString()():0+ this.gethours()));
STR = str.replace( / H | H / g,this.gethours());
STR = str.replace( / /毫米,this.getminutes()> 9this.getminutes()ToString()():0+ this.getminutes()));
STR = str.replace( / / g,this.getminutes());

STR = str.replace( / / | SS SS,this.getseconds()> 9this.getseconds()ToString()():0+ this.getseconds()));
STR = str.replace( / S | S/g,this.getseconds());

STR = str.replace( /三/ g,this.getmilliseconds()<10分+ this.getmilliseconds():(this.getmilliseconds()<100 '0' + this.getmilliseconds():this.getmilliseconds())。

返回字符;
}


*字符串转换为日期类型:
*中:要求的日期字符串,如果不解决日期类型,返回null
*格式:
*(1)YYYY /毫米/ DD:IE和FF的通用
*(2)毫米/日/年:IE和FF的通用
*(3)日期:只有IE
*(4)yyyy-mm-dd:非IE,并解析在八点钟
* /
date.stringtodate =功能(中)
{
如果(!DateStr){
警报(无法解析字符串为日期);
返回null;
其他{ }
如果(date.isvalidate(中,yyyy /毫米/ DD)| | date.isvalidate(中,MM / DD /年)){
Return new Date (Date.parse (dateStr));
其他{ }
如果((!(!- { 1,}){伊江
如果(date.isvalidate(中,日期)){
返回新的日期(Date.parse(中));
其他{ }
警报(无法解析字符串为日期);
返回null;
}
}其他非IE
如果(date.isvalidate(中,yyyy-mm-dd)){
返回新的日期(Date.parse(中));
其他{ }
警报(无法解析字符串为日期);
返回null;
}
}
}
}
返回null;
}


*两个日期计算天数的差异:
* dateone:要求必须是数据类型的一个实例
* datetwo:要求必须是数据类型的一个实例
* /
date.daysbetween =功能(dateone,datetwo)
{
如果((dateone是日期)= =假| |(datetwo是日期)= = false){
返回0;
其他{ }
返回Math.abs((Math.floor((()()dateone.gettime -datetwo.gettime) / 1000 / 60 / 60 / 24)));
}
}


*日期:支持负数,可以增加或减少,返回日期后计算。
*数字:必须是数字的,正值,负数是日期的减少。
*字段:可选的,标识是添加或减去哪个字段,字段看到下面的约定。当没有这个参数时,默认值是d。
*同意以下格式:
*(1)y / y
*(2)m个月
*(3)w/w周
*(4)d天
*(5)h / h
*(6)m分
*(7)秒秒
*(8)q季
* /
date.prototype.dateadd =功能(num,场)
{
如果(!(!努姆)| | isnan(努姆)| | parseInt(努姆)= = 0){
返回此;
}
如果(!场){
字段=d;
}
开关(字段){
case'y:
case'y:返回日期((this.getfullyear()+数字),this.getmonth(),(),this.getdate this.gethours(),(),()this.getminutes this.getseconds);
case'q:
case'q:返回日期(this.getfullyear(),(this.getmonth()+ Num * 3),this.getdate(),(),()this.gethours this.getminutes,this.getseconds());
案例是:返回日期(this.getfullyear(),this.getmonth()+ Num,this.getdate(),(),()this.gethours this.getminutes,this.getseconds());
case'w:
case'w:返回日期(Date.parse(本)+((86400000 * 7)* Num)打破);
案件':
案例会:返回日期(Date.parse(本)+(86400000×NUM)打破);
case'h:
case'h:返回日期(Date.parse(本)+(3600000×Num)打破);
案例是:返回日期(Date.parse(本)+(60000×Num)打破);
案例':
例:返回日期(Date.parse(本)+(1000×NUM)打破);
默认值:返回此;
}
返回此;
}


*比较日期差异:比较两个周期的同一个字段并返回差值
* dtend:要求必须是数据类型的一个实例
*字段:可选,标识是在哪个字段进行比较,字段看到下面的约定。当没有这个参数时,默认值是d
*同意以下格式:
*(1)y / y
*(2)m个月
*(3)w/w周
*(4)d天
*(5)h / h
*(6)m分
*(7)秒秒
* /
date.prototype.datediff =功能(dtend、场)
{
无功dtstart =这;
如果((dtend是日期)= = false){
返回0;
其他{ }
如果(!场){
字段=d;
}
开关(字段){
case'y:
case'y:返回dtend.getfullyear()-();打破dtstart.getfullyear;
案例是:返回(dtend.getmonth)+(1)+(dtend.getfullyear -dtstart.getfullyear()())* 12)-(dtstart.getmonth()+ 1);打破;
case'w:
case'w:返回parseInt((dtend - dtstart)/(86400000×7));突破;
案件':
案例:parseInt(会返回(dtend - dtstart) / 86400000);打破;
case'h:
case'h:返回parseInt((dtend - dtstart) / 3600000);打破;
案例是:返回parseInt((dtend - dtstart) / 60000);打破;
案例':
例:返回parseInt((dtend - dtstart) / 1000);打破;
默认值:返回0;
}
返回0;
}
}


*日期分别被划分为一个数组数组:序列号:根据分钟和秒的日期。
* /
date.prototype.toarray =功能()
{
var =新数组MyArray();
MyArray { 0 } = this.getfullyear();
MyArray { 1 } = this.getmonth();
MyArray { 2 } = this.getdate();
MyArray { 3 } = this.gethours();
MyArray { 4 } = this.getminutes();
MyArray { 5 } = this.getseconds();
返回数组MyArray;
}


*日期数据信息:
*字段:可选,标识是在哪个字段进行比较,字段看到下面的约定。当没有这个参数时,默认值是d
*(1)y / y
*(2)m个月
*(3)w/w周
*(4)d天
*(5)h / h
*(6)m分
*(7)秒秒
* /
date.prototype.datepart =功能(场)
{
如果(!场){
字段=d;
}
var一周=日,A,二,三,四,五,六};
开关(场){
case'y:
case'y:返回this.getfullyear();突破;
案例是:返回(this.getmonth()+ 1);打破;
case'w:
case'w:返回周{ this.getday()};打破;
案件':
例:()会返回this.getdate;打破;
case'h:
Case'H': return this.getHours (); break;
案例是:返回this.getminutes();突破;
例:()返回this.getseconds;打破;
默认值:返回this.getdate();
}
返回this.getdate();
}