1、插件代码
/** * @title 时间工具类 * @note 本类一律违规验证返回false * @author {boonyachengdu@gmail.com} * @date 2013-07-01 * @formatter "2013-07-01 00:00:00" , "2013-07-01", "2013-07-01 23:59:59" * @version LBS2.0
* @param plugin Jtime v1.1
* @notice 兼容性 支持 chrome 、firefox、IE8 (其他浏览器未测试过) */ (function(){ window.TimeObjectUtil={}; 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") */ formatterDateZeroTime : 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 = this.formatterDateTime(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 = this.formatterDateTime(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 sday=new Date(); sday.setDate(sday.getDate()-day); sday.setHours(0, 0, 0, 0); sday=this.formatterDateTime(sday); var eday=new Date(); if(day!=0) eday.setDate(eday.getDate()-1); eday.setHours(23, 59, 59, 59); eday=this.formatterDateTime(eday); var obj = { startTime : sday, endTime : eday }; return obj; }, /** * 获取今天[开始时间和结束时间值] */ getTodayDateTime : function() { var stoday=new Date(); stoday.setHours(0, 0, 0, 0); stoday=this.formatterDateTime(stoday); var etoday=new Date(); etoday.setHours(23, 59, 59, 59); etoday=this.formatterDateTime(etoday); var obj = { startTime : stoday, endTime : etoday }; return obj; }, /** * 获取明天[开始时间和结束时间值] */ getTomorrowDateTime : function() { var stomorrow=new Date(); stomorrow.setDate(stomorrow.getDate()+1); stomorrow.setHours(0, 0, 0, 0); stomorrow=this.formatterDateTime(stomorrow); var etomorrow=new Date(); etomorrow.setDate(etomorrow.getDate()+1); etomorrow.setHours(23, 59, 59, 59); etomorrow=this.formatterDateTime(etomorrow); var obj = { startTime : stomorrow, endTime : etomorrow }; return obj; } }; })();
2、使用配置
/*-----------------------------------------使用配置方式------------------------------------------*/ //执行策略 function executeTimeCase(key){ return TimeObjectUtil.getRecentDaysDateTime(key); //今天0,明天-1,昨天1,前天2,近三天3,近七天7,近n天n,未来n天-n }