jQuery時(shí)間驗(yàn)證和轉(zhuǎn)換為標(biāo)準(zhǔn)格式的時(shí)間格式
最近整理文檔發(fā)現(xiàn)一篇文章,可以將時(shí)間轉(zhuǎn)換成標(biāo)準(zhǔn)格式的時(shí)間,發(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í)間大于開始時(shí)間}
*/
compareDateEndTimeGTStartTime : function(startTime, endTime) {
return ((new Date(endTime.replace(/-/g, "/"))) > (new Date(
startTime.replace(/-/g, "/"))));
},
/**
* 驗(yàn)證開始時(shí)間合理性{開始時(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)證開始時(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)證開始時(shí)間合理性{結(jié)束時(shí)間與開始時(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;
},
/**
* 獲取最近幾天[開始時(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;
},
/**
* 獲取今天[開始時(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;
},
/**
* 獲取明天[開始時(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;
}
};
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用jQuery實(shí)現(xiàn)更改默認(rèn)alert框體
本文給大家介紹的是如何使用jquery 改寫Alert彈出框樣式,十分的實(shí)用,這里推薦給大家,有需要的小伙伴可以參考下。2015-04-04
10分鐘學(xué)會(huì)寫Jquery插件實(shí)例教程
這篇文章主要介紹了10分鐘學(xué)會(huì)寫Jquery插件,通過一個(gè)實(shí)例分兩步完成一個(gè)簡(jiǎn)單的插件,非常實(shí)用,需要的朋友可以參考下2014-09-09
jQuery實(shí)現(xiàn)單擊按鈕遮罩彈出對(duì)話框(仿天貓的刪除對(duì)話框)
單擊刪除按鈕或者登陸按鈕后,彈出對(duì)話框問你是否刪除或者彈出一個(gè)登陸對(duì)話框,本文使用jquery來實(shí)現(xiàn)這種效果,需要的朋友可以參考下2014-04-04
Jquery和CSS實(shí)現(xiàn)選擇框重置按鈕功能
在本篇文章中我們給大家?guī)砹薐query和CSS實(shí)現(xiàn)選擇框重置按鈕功能的相關(guān)代碼,需要的朋友們參考下。2018-11-11
Jquery通過Ajax方式來提交Form表單的具體實(shí)現(xiàn)
提交Form表單的方法有很多,在本文為大家介紹下Jquery通過Ajax方式是如何提交Form表單的2013-11-11
jQuery插件FusionWidgets實(shí)現(xiàn)的Bulb圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件FusionWidgets實(shí)現(xiàn)的Bulb圖效果,結(jié)合完整實(shí)例形式分析了jQuery使用FusionWidgets插件結(jié)合swf文件載入xml數(shù)據(jù)實(shí)現(xiàn)Bulb圖效果的相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03
jQuery實(shí)現(xiàn)中獎(jiǎng)播報(bào)功能(讓文本滾動(dòng)起來) 簡(jiǎn)單設(shè)置數(shù)值即可
這篇文章主要介紹了jQuery實(shí)現(xiàn)中獎(jiǎng)播報(bào)功能(讓文本滾動(dòng)起來) 簡(jiǎn)單設(shè)置數(shù)值即可,文中通過實(shí)例代碼給大家詳細(xì)介紹,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03

