自己整理的一個javascript日期處理函數

來源:互聯網
上載者:User

複製代碼 代碼如下:/*
* 函數名稱: DateUtil
* 作 者: yithcn
* 功能說明: 日期函數
* 使用說明:
* 建立日期: 2010.10.14
*/
var DateUtil = {};
DateUtil.base = 60 * 60 * 24 * 1000;
DateUtil.Add = function(num, sDate) {
num = num || 0;
sDate = sDate || new Date();
var base = this.base * num;
var todayMs = sDate.getTime();
todayMs += base;
sDate.setTime(todayMs);
var m = (sDate.getMonth() + 1);
m = m < 10 ? "0" + m : m;
var d = sDate.getDate();
d = d < 10 ? "0" + d : d;
var y = sDate.getFullYear();
return m + "/" + d + "/" + y;
};
DateUtil.Diff = function(sDate, eDate, mode) {
if (typeof sDate == "string")
sDate = new Date(sDate);
if (typeof eDate == "string")
eDate = new Date(eDate);
sDate = sDate || new Date();
eDate = eDate || new Date();
try {
sDate.getYear();
} catch (e) {
return (0);
}
var result = Math.abs(eDate - sDate);
switch (mode) {
case "y":
result /= this.base * 365;
break;
case "m":
result /= this.base * 365 / 12;
break;
case "w":
result /= this.base * 7;
break;
default:
result /= this.base;
break;
}
return (Math.floor(result));
};
DateUtil.Time = function(hasSec) {
var date = new Date();
return date.getHours() + ":" + date.getMinutes() + (hasSec ? ":" + date.getSeconds() : "");
};
DateUtil.TimeSplit = function(hasSec) {
var date = new Date();
return { Hour: date.getHours(), Minute: date.getMinutes(), Second: (hasSec ? ":" + date.getSeconds() : "") };
};
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.