教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 js日期与时间比较函数多实例

js日期与时间比较函数多实例

发布时间:2015-06-18   编辑:jiaochengji.com
分享下js中日期与时间比较函数的多个例子,学习下js日期函数、js时间函数的用法,有需要的朋友参考下。

例1,js日期比较(yyyy-mm-dd)
 

复制代码 代码示例:

function duibi(a, b) {
    var arr = a.split("-");
    var starttime = new date(arr[0], arr[1], arr[2]);
    var starttimes = starttime.gettime();

    var arrs = b.split("-");
    var lktime = new date(arrs[0], arrs[1], arrs[2]);
    var lktimes = lktime.gettime();

    if (starttimes >= lktimes) {

        alert('开始时间大于离开时间,请检查');
        return false;
    }
    else
        return true;
}

例2,js时间比较(yyyy-mm-dd hh:mi:ss)函数用法
 

复制代码 代码示例:

function comptime() {
    var begintime = "2009-09-21 00:00:00";
    var endtime = "2009-09-21 00:00:01";
    var begintimes = begintime.substring(0, 10).split('-');
    var endtimes = endtime.substring(0, 10).split('-');

    begintime = begintimes[1] + '-' + begintimes[2] + '-' + begintimes[0] + ' ' + begintime.substring(10, 19);
    endtime = endtimes[1] + '-' + endtimes[2] + '-' + endtimes[0] + ' ' + endtime.substring(10, 19);

    alert(begintime + "aaa" + endtime);
    alert(date.parse(endtime)); //(教程集 www.jiaochengji.com 编辑整理)
    alert(date.parse(begintime));
    var a = (date.parse(endtime) - date.parse(begintime)) / 3600 / 1000;
    if (a < 0) {
        alert("endtime小!");
    } else if (a > 0) {
        alert("endtime大!");
    } else if (a == 0) {
        alert("时间相等!");
    } else {
        return 'exception'
    }
}

例3,js日期计算函数
 

复制代码 代码示例:
function dateadd(date1, day) {
var t = new date();
   if (day < 0) day = 0;
   var t = date.parse(date1) + day * 1000 * 3600 * 24;
   t.settime(t);
   return t;
}

您可能感兴趣的文章:
js日期与时间比较函数多实例
javascript日期计算与格式化日期
js时间转换(毫秒转换成日期时间)
js比较两个日期时间大小的例子
js获取当前时间戳与日期比较
js date日期函数的例子
mysql常用的日期加减函数与实例教程
js按指定格式显示日期时间的代码
php日期函数的简单示例代码
PHP 5.2日期、时间和时区处理详解

关键词: js日期函数  日期比较  时间比较   
[关闭]
~ ~