您的位置:首页 > Web前端 > JavaScript

Js 时间处理总结

2013-02-18 14:34 330 查看
1.获取当前时间

1 function getNowTime() {
2             return new Date();
3         }


2.时间与天数相加

1 function getTimeAddDays(time, days) {
2             return new Date(time.getTime() + days * 24 * 60 * 60 * 1000);
3         }


3.获取并格式化日期:年-月-日

1  function getFormatDate(time) {
2             return time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate();
3         }


4.字符串转换为日期,字符串格式:2011-11-20

function convertToDate(strings) {
return new Date(Date.parse(strings.replace("-", "/")));
}


5.获取并格式化星期

1  var WEEKDAYS = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]; //星期
2
3  function getFormatWeek(time) {
4             return WEEKDAYS[time.getDay()];
5         }


6.时间比较

1  function compareTime(time1, time2) {
2             return time1.getTime() - time2.getTime();
3         }


7.计算两个日期之间相隔的天数

function getDays(time1, tiem2){
var day = 24*60*60*1000;
return (time1.getTime() - time2.getTime())/day;
}


http://www.cnblogs.com/liuhaorain/archive/2011/12/31/2308964.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: