tinydate.js[v0.1]關於Javascript Date的工具
阿新 • • 發佈:2018-06-14
orm || def 代碼復用 eth clas subst bst ini
編寫初衷
JavaScript的Date對象所提供的方法無法滿足生產的需要,比如他的Month,他的日期加減,比如我要得到1993年1月1日到現在有多少天,比如我想知道我在這個世界上活了多少個小時,那麽都需要“換算”。先得到時間戳,然後在換算~~~
- 其實只是將常用的操作,寫成了js,代碼復用減輕工作量。
它的功能
- TimeStamp
直接獲取某個時間戳、時間對象、時間字符串的年月日時分秒毫秒,完整的年月日時分秒毫秒。 - 計算時間差
調用時間差函數會計算兩個時間的差值,返回值是 TimeStamp對象(自定義的) - 日期格式化
調用格式化函數可以對日期進行格式化 - 添加年、月、日、時、分、秒
將Date原有的setYear...等函數進行拓展,時常會有一種需求去添加某個類型的時間,但是~經常要我們寫成date.setYear(date.getFullYear()+year);
date.addYear(year);
即可。
代碼
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"H+": this.getHours(), //小時
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"f+": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}
//當前完整時間
Date.prototype.$nowDate = new Date().format("yyyy-MM-dd HH:mm:ss.ffff");
//獲取自定義格式的當前時間
Date.prototype.$now = function (fmt) {
return new Date().format(fmt);
}
//計算時間差
Date.prototype.diff = function (sDate, eDate) {
if (eDate == undefined || eDate == null)
eDate = new Date();
var stime = sDate.getTime();
var etime = eDate.getTime();
var diffTime = etime - stime;
var timeSpan = new TimeSpan(diffTime);
return timeSpan;
}
//添加年
Date.prototype.addYear = function (number) {
this.setFullYear(this.getFullYear() + number);
}
//添加月
Date.prototype.addMonth = function (number){
this.setMonth(this.getMonth()+number);
}
//添加日
Date.prototype.addDate = function (number){
this.setDate(this.getDate()+number);
}
//添加小時
Date.prototype.addHours = function (number){
this.setHours(this.getHours()+number);
}
//添加分
Date.prototype.addMinutes = function (number){
this.setMinutes(this.getMinutes()+number);
}
//添加秒
Date.prototype.addSeconds = function (number){
this.setSeconds(this.getSeconds()+number);
}
//添加毫秒
Date.prototype.addMilliseconds = function (number){
this.setMilliseconds(this.getMilliseconds()+number);
}
//獲得一年中第一天的日期
Date.prototype.getTheFirstDateOfTheYear = function (date) {
var year, month=0, day=1;
if (date == undefined || date == null) {
year = this.getFullYear();
}
else {
year = date.getFullYear();
}
return new Date(year, month, day);
}
//獲得一年中最後一天的日期
Date.prototype.getTheLastDateOfTheYear = function (date) {
var year, month = 11, day = 31;
if (date == undefined || date == null) {
year = this.getFullYear();
}
else {
year = date.getFullYear();
}
return new Date(year, month, day);
}
//格式化當前日期 為 時限對象
Date.prototype.timeSpan = function () {
return new TimeSpan(this);
}
//時限對象
function TimeSpan() {
var o = new Object();
o.year = 0;//年
o.month = 0;//月
o.day = 0;//日
o.hours = 0;//時
o.minutes = 0;//分
o.seconds = 0;//秒
o.milliseconds = 0;//毫秒
o.totalYear = 0.00;//從時間原點的年
o.totalMonth = 0.00;//從時間原點到現在的月
o.totalDay = 0.00;//從時間原點到現在的天
o.totalHours = 0.00;//從時間原點到現在的小時
o.totalMinutes = 0.00;//從時間原點到現在的分
o.totalSeconds = 0.00;//從時間原點到現在的秒
o.totalMilliseconds = 0.00;//從時間原點到現在的毫秒
//初始化對象
o.init = function (timestamp) {
var odate = new Date(timestamp);
o.year = odate.getFullYear();
o.month = odate.getMonth() + 1;
o.day = odate.getDate();
o.hours = odate.getHours();
o.minutes = odate.getMinutes();
o.seconds = odate.getSeconds();
o.milliseconds = odate.getMilliseconds();
o.totalMilliseconds = timestamp;
o.totalSeconds = (timestamp / 1000).toFixed(2);
o.totalMinutes = (timestamp / 1000 / 60).toFixed(2);
o.totalHours = (timestamp / 1000 / 60 / 60).toFixed(2);
o.totalDay = (timestamp / 1000 / 60 / 60 / 24).toFixed(2);
o.totalMonth = o.year * 12;
o.totalYear = o.year;
}
//無參則返回空對象
if (arguments.length == 0) {
}
else if (typeof (arguments[0]) === "string") {
o.init(new Date(arguments[0]));
}
else if (typeof (arguments[0]) === "number") {
o.init(arguments[0]);
} else if (typeof(arguments[0]) === "object") {
o.init(arguments[0]);
}
return o;
}
tinydate.js[v0.1]關於Javascript Date的工具