一聚教程网:一个值得你收藏的教程网站

热门教程

jQuery时间验证和转换为标准格式的时间格式

时间:2022-06-25 17:07:18 编辑:袖梨 来源:一聚教程网

 

 代码如下复制代码

varTimeObjectUtil;

/**

 * @title 时间工具类

 * @note 本类一律违规验证返回false

 * @author {[email protected]}

 * @date 2013-07-01

 * @formatter "2013-07-01 00:00:00" , "2013-07-01"

 */

TimeObjectUtil = {

  /**

   * 获取当前时间毫秒数

   */

  getCurrentMsTime :function() {

    varmyDate =newDate();

    returnmyDate.getTime();

  },

  /**

   * 毫秒转时间格式

   */

  longMsTimeConvertToDateTime :function(time) {

    varmyDate =newDate(time);

    returnthis.formatterDateTime(myDate);

  },

  /**

   * 时间格式转毫秒

   */

  dateToLongMsTime :function(date) {

    varmyDate =newDate(date);

    returnmyDate.getTime();

  },

  /**

   * 格式化日期(不含时间)

   */

  formatterDate :function(date) {

    vardatetime = date.getFullYear()

        +"-"// "年"

        + ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) :"0"

            + (date.getMonth() + 1))

        +"-"// "月"

        + (date.getDate() < 10 ?"0"+ date.getDate() : date

            .getDate());

    returndatetime;

  },

  /**

   * 格式化日期(含时间"00:00:00")

   */

  formatterDate2 :function(date) {

    vardatetime = date.getFullYear()

        +"-"// "年"

        + ((date.getMonth() + 1) > 10 ? (date.getMonth() + 1) :"0"

            + (date.getMonth() + 1))

        +"-"// "月"

        + (date.getDate() < 10 ?"0"+ date.getDate() : date

            .getDate()) +" "+"00:00:00";

    returndatetime;

  },

  /**

   * 格式化去日期(含时间)

   */

  formatterDateTime :function(date) {

    vardatetime = 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());

    returndatetime;

  },

  /**

   * 时间比较{结束时间大于开始时间}

   */

  compareDateEndTimeGTStartTime :function(startTime, endTime) {

    return((newDate(endTime.replace(/-/g,"/"))) > (newDate(

        startTime.replace(/-/g,"/"))));

  },

  /**

   * 验证开始时间合理性{开始时间不能小于当前时间{X}个月}

   */

  compareRightStartTime :function(month, startTime) {

    varnow = formatterDayAndTime(newDate());

    varsms =newDate(startTime.replace(/-/g,"/"));

    varems =newDate(now.replace(/-/g,"/"));

    vartDayms = month * 30 * 24 * 60 * 60 * 1000;

    vardvalue = ems - sms;

    if(dvalue > tDayms) {

      returnfalse;

    }

    returntrue;

  },

  /**

   * 验证开始时间合理性{结束时间不能小于当前时间{X}个月}

   */

  compareRightEndTime :function(month, endTime) {

    varnow = formatterDayAndTime(newDate());

    varsms =newDate(now.replace(/-/g,"/"));

    varems =newDate(endTime.replace(/-/g,"/"));

    vartDayms = month * 30 * 24 * 60 * 60 * 1000;

    vardvalue = sms - ems;

    if(dvalue > tDayms) {

      returnfalse;

    }

    returntrue;

  },

  /**

   * 验证开始时间合理性{结束时间与开始时间的间隔不能大于{X}个月}

   */

  compareEndTimeGTStartTime :function(month, startTime, endTime) {

    varsms =newDate(startTime.replace(/-/g,"/"));

    varems =newDate(endTime.replace(/-/g,"/"));

    vartDayms = month * 30 * 24 * 60 * 60 * 1000;

    vardvalue = ems - sms;

    if(dvalue > tDayms) {

      returnfalse;

    }

    returntrue;

  },

  /**

   * 获取最近几天[开始时间和结束时间值,时间往前推算]

   */

  getRecentDaysDateTime :function(day) {

    vardaymsTime = day * 24 * 60 * 60 * 1000;

    varyesterDatsmsTime =this.getCurrentMsTime() - daymsTime;

    varstartTime =this.longMsTimeConvertToDateTime(yesterDatsmsTime);

    varpastDate =this.formatterDate2(newDate(startTime));

    varnowDate =this.formatterDate2(newDate());

    varobj = {

      startTime : pastDate,

      endTime : nowDate

    };

    returnobj;

  },

  /**

   * 获取今天[开始时间和结束时间值]

   */

  getTodayDateTime :function() {

    vardaymsTime = 24 * 60 * 60 * 1000;

    vartomorrowDatsmsTime =this.getCurrentMsTime() + daymsTime;

    varcurrentTime =this.longMsTimeConvertToDateTime(this.getCurrentMsTime());

    vartermorrowTime =this.longMsTimeConvertToDateTime(tomorrowDatsmsTime);

    varnowDate =this.formatterDate2(newDate(currentTime));

    vartomorrowDate =this.formatterDate2(newDate(termorrowTime));

    varobj = {

      startTime : nowDate,

      endTime : tomorrowDate

    };

    returnobj;

  },

  /**

   * 获取明天[开始时间和结束时间值]

   */

  getTomorrowDateTime :function() {

    vardaymsTime = 24 * 60 * 60 * 1000;

    vartomorrowDatsmsTime =this.getCurrentMsTime() + daymsTime;

    vartermorrowTime =this.longMsTimeConvertToDateTime(tomorrowDatsmsTime);

    vartheDayAfterTomorrowDatsmsTime =this.getCurrentMsTime()+ (2 * daymsTime);

    vartheDayAfterTomorrowTime =this.longMsTimeConvertToDateTime(theDayAfterTomorrowDatsmsTime);

    varpastDate =this.formatterDate2(newDate(termorrowTime));

    varnowDate =this.formatterDate2(newDate(theDayAfterTomorrowTime));

    varobj = {

      startTime : pastDate,

      endTime : nowDate

    };

    returnobj;

  }

};

 

热门栏目