JavaScript中的时间处理小结
废话不多说了,主要通过以下七个方面给大家总结了时间处理相关知识。
1.获取当前时间
functiongetNowTime(){
returnnewDate();
}
2.时间与天数相加
functiongetTimeAddDays(time,days){
returnnewDate(time.getTime()+days*24*60*60*1000);
}
3.获取并格式化日期:年-月-日
functiongetFormatDate(time){
returntime.getFullYear()+"-"+(time.getMonth()+1)+"-"+time.getDate();
}
4.字符串转换为日期,字符串格式:2011-11-20
functionconvertToDate(strings){
returnnewDate(Date.parse(strings.replace("-","/")));
}
5.获取并格式化星期
varWEEKDAYS=["周日","周一","周二","周三","周四","周五","周六"];//星期
functiongetFormatWeek(time){
returnWEEKDAYS[time.getDay()];
}
6.时间比较
functioncompareTime(time1,time2){
returntime1.getTime()-time2.getTime();
}
7.计算两个日期之间相隔的天数
functiongetDays(time1,tiem2){
varday=24*60*60*1000;
return(time1.getTime()-time2.getTime())/day;
}
小编给大家总结了七个方面有关js中时间处理知识,希望对大家有所帮助!
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短