欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Jquery时间验证和转换工具小例子

程序员文章站 2023-03-26 23:02:11
代码如下: var timeobjectutil; /** * @title 时间工具类 * @note 本类一律违规验证返回false * @author {boonyachengdu@gmai...

代码如下:


var timeobjectutil;
/**
* @title 时间工具类
* @note 本类一律违规验证返回false
* @author {boonyachengdu@gmail.com}
* @date 2013-07-01
* @formatter "2013-07-01 00:00:00" , "2013-07-01"
*/
timeobjectutil = {
/**
* 获取当前时间毫秒数
*/
getcurrentmstime : function() {
var mydate = new date();
return mydate.gettime();
},
/**
* 毫秒转时间格式
*/
longmstimeconverttodatetime : function(time) {
var mydate = new date(time);
return this.formatterdatetime(mydate);
},
/**
* 时间格式转毫秒
*/
datetolongmstime : function(date) {
var mydate = new date(date);
return mydate.gettime();
},
/**
* 格式化日期(不含时间)
*/
formatterdate : function(date) {
var datetime = date.getfullyear()
+ "-"// "年"
+ ((date.getmonth() + 1) > 10 ? (date.getmonth() + 1) : "0"
+ (date.getmonth() + 1))
+ "-"// "月"
+ (date.getdate() < 10 ? "0" + date.getdate() : date
.getdate());
return datetime;
},
/**
* 格式化日期(含时间"00:00:00")
*/
formatterdate2 : function(date) {
var datetime = date.getfullyear()
+ "-"// "年"
+ ((date.getmonth() + 1) > 10 ? (date.getmonth() + 1) : "0"
+ (date.getmonth() + 1))
+ "-"// "月"
+ (date.getdate() < 10 ? "0" + date.getdate() : date
.getdate()) + " " + "00:00:00";
return datetime;
},
/**
* 格式化去日期(含时间)
*/
formatterdatetime : function(date) {
var datetime = date.getfullyear()
+ "-"// "年"
+ ((date.getmonth() + 1) > 10 ? (date.getmonth() + 1) : "0"
+ (date.getmonth() + 1))
+ "-"// "月"
+ (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;
},
/**
* 时间比较{结束时间大于开始时间}
*/
comparedateendtimegtstarttime : function(starttime, endtime) {
return ((new date(endtime.replace(/-/g, "/"))) > (new date(
starttime.replace(/-/g, "/"))));
},
/**
* 验证开始时间合理性{开始时间不能小于当前时间{x}个月}
*/
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;
},
/**
* 验证开始时间合理性{结束时间不能小于当前时间{x}个月}
*/
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;
},
/**
* 验证开始时间合理性{结束时间与开始时间的间隔不能大于{x}个月}
*/
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;
},
/**
* 获取最近几天[开始时间和结束时间值,时间往前推算]
*/
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;
},
/**
* 获取今天[开始时间和结束时间值]
*/
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;
},
/**
* 获取明天[开始时间和结束时间值]
*/
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;
}
};