Jquery時(shí)間驗(yàn)證和轉(zhuǎn)換工具小例子
更新時(shí)間:2013年07月01日 15:03:21 作者:
這篇文章介紹了Jquery時(shí)間驗(yàn)證和轉(zhuǎn)換工具小例子,有需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
var TimeObjectUtil;
/**
* @title 時(shí)間工具類
* @note 本類一律違規(guī)驗(yàn)證返回false
* @author {boonyachengdu@gmail.com}
* @date 2013-07-01
* @formatter "2013-07-01 00:00:00" , "2013-07-01"
*/
TimeObjectUtil = {
/**
* 獲取當(dāng)前時(shí)間毫秒數(shù)
*/
getCurrentMsTime : function() {
var myDate = new Date();
return myDate.getTime();
},
/**
* 毫秒轉(zhuǎn)時(shí)間格式
*/
longMsTimeConvertToDateTime : function(time) {
var myDate = new Date(time);
return this.formatterDateTime(myDate);
},
/**
* 時(shí)間格式轉(zhuǎn)毫秒
*/
dateToLongMsTime : function(date) {
var myDate = new Date(date);
return myDate.getTime();
},
/**
* 格式化日期(不含時(shí)間)
*/
formatterDate : function(date) {
var datetime = date.getFullYear()
+ "-"http:// "年"
+ ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0"
+ (date.getMonth() + 1))
+ "-"http:// "月"
+ (date.getDate() < 10 ? "0" + date.getDate() : date
.getDate());
return datetime;
},
/**
* 格式化日期(含時(shí)間"00:00:00")
*/
formatterDate2 : function(date) {
var datetime = date.getFullYear()
+ "-"http:// "年"
+ ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0"
+ (date.getMonth() + 1))
+ "-"http:// "月"
+ (date.getDate() < 10 ? "0" + date.getDate() : date
.getDate()) + " " + "00:00:00";
return datetime;
},
/**
* 格式化去日期(含時(shí)間)
*/
formatterDateTime : function(date) {
var datetime = date.getFullYear()
+ "-"http:// "年"
+ ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0"
+ (date.getMonth() + 1))
+ "-"http:// "月"
+ (date.getDate() < 10 ? "0" + date.getDate() : date
.getDate())
+ " "
+ (date.getHours() < 10 ? "0" + date.getHours() : date
.getHours())
+ ":"
+ (date.getMinutes() < 10 ? "0" + date.getMinutes() : date
.getMinutes())
+ ":"
+ (date.getSeconds() < 10 ? "0" + date.getSeconds() : date
.getSeconds());
return datetime;
},
/**
* 時(shí)間比較{結(jié)束時(shí)間大于開(kāi)始時(shí)間}
*/
compareDateEndTimeGTStartTime : function(startTime, endTime) {
return ((new Date(endTime.replace(/-/g, "/"))) > (new Date(
startTime.replace(/-/g, "/"))));
},
/**
* 驗(yàn)證開(kāi)始時(shí)間合理性{開(kāi)始時(shí)間不能小于當(dāng)前時(shí)間{X}個(gè)月}
*/
compareRightStartTime : function(month, startTime) {
var now = formatterDayAndTime(new Date());
var sms = new Date(startTime.replace(/-/g, "/"));
var ems = new Date(now.replace(/-/g, "/"));
var tDayms = month * 30 * 24 * 60 * 60 * 1000;
var dvalue = ems - sms;
if (dvalue > tDayms) {
return false;
}
return true;
},
/**
* 驗(yàn)證開(kāi)始時(shí)間合理性{結(jié)束時(shí)間不能小于當(dāng)前時(shí)間{X}個(gè)月}
*/
compareRightEndTime : function(month, endTime) {
var now = formatterDayAndTime(new Date());
var sms = new Date(now.replace(/-/g, "/"));
var ems = new Date(endTime.replace(/-/g, "/"));
var tDayms = month * 30 * 24 * 60 * 60 * 1000;
var dvalue = sms - ems;
if (dvalue > tDayms) {
return false;
}
return true;
},
/**
* 驗(yàn)證開(kāi)始時(shí)間合理性{結(jié)束時(shí)間與開(kāi)始時(shí)間的間隔不能大于{X}個(gè)月}
*/
compareEndTimeGTStartTime : function(month, startTime, endTime) {
var sms = new Date(startTime.replace(/-/g, "/"));
var ems = new Date(endTime.replace(/-/g, "/"));
var tDayms = month * 30 * 24 * 60 * 60 * 1000;
var dvalue = ems - sms;
if (dvalue > tDayms) {
return false;
}
return true;
},
/**
* 獲取最近幾天[開(kāi)始時(shí)間和結(jié)束時(shí)間值,時(shí)間往前推算]
*/
getRecentDaysDateTime : function(day) {
var daymsTime = day * 24 * 60 * 60 * 1000;
var yesterDatsmsTime = this.getCurrentMsTime() - daymsTime;
var startTime = this.longMsTimeConvertToDateTime(yesterDatsmsTime);
var pastDate = this.formatterDate2(new Date(startTime));
var nowDate = this.formatterDate2(new Date());
var obj = {
startTime : pastDate,
endTime : nowDate
};
return obj;
},
/**
* 獲取今天[開(kāi)始時(shí)間和結(jié)束時(shí)間值]
*/
getTodayDateTime : function() {
var daymsTime = 24 * 60 * 60 * 1000;
var tomorrowDatsmsTime = this.getCurrentMsTime() + daymsTime;
var currentTime = this.longMsTimeConvertToDateTime(this.getCurrentMsTime());
var termorrowTime = this.longMsTimeConvertToDateTime(tomorrowDatsmsTime);
var nowDate = this.formatterDate2(new Date(currentTime));
var tomorrowDate = this.formatterDate2(new Date(termorrowTime));
var obj = {
startTime : nowDate,
endTime : tomorrowDate
};
return obj;
},
/**
* 獲取明天[開(kāi)始時(shí)間和結(jié)束時(shí)間值]
*/
getTomorrowDateTime : function() {
var daymsTime = 24 * 60 * 60 * 1000;
var tomorrowDatsmsTime = this.getCurrentMsTime() + daymsTime;
var termorrowTime = this.longMsTimeConvertToDateTime(tomorrowDatsmsTime);
var theDayAfterTomorrowDatsmsTime = this.getCurrentMsTime()+ (2 * daymsTime);
var theDayAfterTomorrowTime = this.longMsTimeConvertToDateTime(theDayAfterTomorrowDatsmsTime);
var pastDate = this.formatterDate2(new Date(termorrowTime));
var nowDate = this.formatterDate2(new Date(theDayAfterTomorrowTime));
var obj = {
startTime : pastDate,
endTime : nowDate
};
return obj;
}
};
相關(guān)文章
jQuery設(shè)置div一直在頁(yè)面頂部顯示的方法
如何讓div一直在頁(yè)面頂部,這種類似的效果大家都有見(jiàn)過(guò)的,實(shí)現(xiàn)的方法也有很多,在本文為大家詳細(xì)介紹下使用jquery是如何實(shí)現(xiàn)的,感興趣的朋友不要錯(cuò)過(guò)2013-10-10jQuery實(shí)現(xiàn)的手風(fēng)琴側(cè)邊菜單效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)的手風(fēng)琴側(cè)邊菜單效果,涉及jQuery事件響應(yīng)及元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03JQuery勾選指定name的復(fù)選框集合并顯示的方法
這篇文章主要介紹了JQuery勾選指定name的復(fù)選框集合并顯示的方法,涉及jQuery表單元素操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05jQuery代碼實(shí)現(xiàn)實(shí)時(shí)獲取時(shí)間
這篇文章主要介紹了jQuery代碼實(shí)現(xiàn)實(shí)時(shí)獲取時(shí)間功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01jQuery中slidedown與slideup方法用法示例
這篇文章主要介紹了jQuery中slidedown與slideup方法用法,結(jié)合實(shí)例形式分析了jQuery基于slidedown與slideup方法實(shí)現(xiàn)頁(yè)面元素展開(kāi)與折疊的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-09-09jQuery實(shí)現(xiàn)HTML表格單元格的合并功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)HTML表格單元格的合并功能,可合并指定行與指定列上的單元格,涉及jQuery針對(duì)表格元素屬性的動(dòng)態(tài)操作技巧,需要的朋友可以參考下2016-04-04