js获取时间并执行相应操作如:计算两个时间之差

  js获取时间并执行相应操作如:计算两个时间之差

  最近在做一个项目的时候,需要在前台利用js获取到时间并进行相应的操作.本来想用jquery的,但是后来发现好像没有封装起来只能使用js了

获取系统时间函数:  

<span style="font-family:SimSun;">//js中的日期时间函数
var date = new Date();
date.getYear();        //获取年份(2位)
date.getFullYear();    //获取完整的年份(4位,1970-)
date.getMonth();       //获取月份(0-11,0代表1月,所以在显示当前时间的时候需要date.getMonth() + 1)
date.getDate();        //获取日(1-31)
date.getDay();         //获取星期?(0-6,0代表星期天)
date.getTime();        //获取时间(从1970.1.1开始的毫秒数)
date.getHours();       //获取小时数(0-23)
date.getMinutes();     //获取分钟数(0-59)
date.getSeconds();     //获取秒数(0-59)
date.getMilliseconds();    //获取毫秒数(0-999)
date.toLocaleDateString();     //获取日期
var time=date.toLocaleTimeString();     //获取时间
date.toLocaleString();        //获取日期与时间</span>

//获取年月日   获取格式为yyyy-MM-dd

<span style="font-family:SimSun;">var currentTime ="";
currentTime = (myDate.getFullYear() + "-");
currentTime = currentTime + ((myDate.getMonth() + 1) + "-");
currentTime = currentTime + (myDate.getDate());</span>

因为两个时间之差只有日,时,分,秒的说法,没有年和月,因为后者为不定值,所以必须把时间合适进行转换为MM-dd-yyyy   当然上面的代码也可以直接拼接成MM-dd-yyyy格式。

                             

<span style="font-family:SimSun;">   var currentTime1 = currentTime.split("-");
   var currentTime2 = new Date(currentTime[1]+"-"+currentTime[2]+"-"+currentTime[0]);//这种格式在火狐中不通过:</span>
<span style="font-family:SimSun;">    </span><pre style="margin-top: 0px; margin-bottom: 0px; margin-left: 22px; white-space: pre-wrap; word-wrap: break-word; line-height: 21.6000003814697px; font-family: 'Courier New' !important;"><span style="line-height: 1.5 !important;">aDate </span>= sDate1.split("-"<span style="line-height: 1.5 !important;">);</span>
oDate1 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]);   //转换为12/13/2008格式
aDate = sDate2.split("-");
oDate2 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]);//这种格式才能通过
 

完整代码:

* 计算两个日期的间隔天数
            * BeginDate:起始日期的文本框,格式為:2012-01-01
            * EndDate:結束日期的文本框,格式為:2012-01-02
            * 返回兩個日期所差的天數
            * 調用方法:
            * alert("相差"+Computation("date1","date2")+"天");
            */            
            function GetDateRegion(BeginDate,EndDate)
            {
                
                var aDate, oDate1, oDate2, iDays;
                var sDate1=document.getElementById(BeginDate).value;   //sDate1和sDate2是2008-12-13格式
               var sDate2=document.getElementById(EndDate).value;              
                aDate = sDate1.split("-");
                oDate1 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]);   //转换为12/13/2008格式
                aDate = sDate2.split("-");
                oDate2 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]);
                //iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24)+1;   //把相差的毫秒数转换为天数
                var i=(oDate1 - oDate2) / 1000 / 60 / 60 /24;
                if(i<0)
                {
                    i-=1;
                }
                else
                {
                    i+=1;
                }
                iDays = i;   //把相差的毫秒数转换为天数
                //alert(iDays);
                return iDays;
            }


假如现在有另外一个时间 那么我们现在只需要得到他们之间相差的毫秒数,就能得到他们之间任意的时间差。oDate1,oDate2都是MM-dd-yyyy格式

        var TotalMilliseconds = Math.abs(oDate1 - oDate2);//相差的毫秒数    最重要的函数就是abs(Number x);

   得到相差天数 var day = TotalMilliseconds/1000/60/60/24;

   得到相差小时 var hour = TotalMilliseconds/1000/60/60;

   其他类似

提供几个常用函数:

判断两个日期的大小(实例:用于计划完成日期和当天时间进行比较)

<span style="font-family:SimSun;">var planTimeTest = '${planTime1}';//获得数据的日期数据格式为 yyyy-MM-dd hh:MM:ss 这是我自己根据业务从后台获取的
planTime1 = planTimeTest.split(" ")[0];//获取计划完成时间yyyy-MM-dd
planCompleteTime = planTime1.split("-");
//获取年月日
currentTime = (myDate.getFullYear() + "-");
currentTime = currentTime + ((myDate.getMonth() + 1) + "-");
currentTime = currentTime + (myDate.getDate());
//比较两个日期的大小,值得注意的是要把月份放在第一位
if(Date.parse((myDate.getMonth() + 1)+"/"+myDate.getDate()+"/"+myDate.getFullYear())>
Date.parse(planCompleteTime[1]+"/"+planCompleteTime[2]+"/"+planCompleteTime[0])){
//做自己的操作
}</span>



//| 把日期分割成数组  

<span style="font-family:SimSun;"><code class="javascript comments" style="font-size:14px; white-space:nowrap; list-style:none; line-height:15.3999996185303px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; padding:0px!important; margin:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background-color:initial!important">Date.prototype.toArray = function()  
{   
    var myDate = this;  
    var myArray = Array();  
    myArray[0] = myDate.getFullYear();  
    myArray[1] = myDate.getMonth();  
    myArray[2] = myDate.getDate();  
    myArray[3] = myDate.getHours();  
    myArray[4] = myDate.getMinutes();  
    myArray[5] = myDate.getSeconds();  
    return myArray;  
}<span style="color:#008200;"> </span></code></span>


//| 取得当前日期所在月的最大天数  

<span style="font-family:SimSun;"><code class="javascript comments" style="font-size:14px; white-space:nowrap; list-style:none; line-height:15.3999996185303px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; padding:0px!important; color:rgb(0,130,0)!important; margin:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none!important">Date.prototype.MaxDayOfDate = function()  
{   
    var myDate = this;  
    var ary = myDate.toArray();  
    var date1 = (new Date(ary[0],ary[1]+1,1));  
    var date2 = date1.dateAdd(1,'m',1);  
    var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd'));  
    return result;  
} </code></span>

//| 取得当前日期所在周是一年中的第几周  

<span style="font-family:SimSun;"><code class="javascript comments" style="font-size:14px; white-space:nowrap; list-style:none; line-height:15.3999996185303px; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; padding:0px!important; color:rgb(0,130,0)!important; margin:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none!important">Date.prototype.WeekNumOfYear = function()  
{   
    var myDate = this;  
    var ary = myDate.toArray();  
    var year = ary[0];  
    var month = ary[1]+1;  
    var day = ary[2];  
    document.write('< script language=VBScript\> \n');  
    document.write('myDate = Datue(''+month+'-'+day+'-'+year+'') \n');  
    document.write('result = DatePart('ww', myDate) \n');  
    document.write(' \n');  
    return result;  
}  </code></span>

若要显示:当前日期加时间(如:2009-06-12 12:00)

<span style="font-family:SimSun;">function CurentTime()
    { 
        var now = new Date();
        var year = now.getFullYear();       //年
        var month = now.getMonth() + 1;     //月
        var day = now.getDate();            //日
        var hh = now.getHours();            //时
        var mm = now.getMinutes();          //分
        var clock = year + "-";
        if(month < 10)
            clock += "0";
        clock += month + "-";
        if(day < 10)
            clock += "0";
        clock += day + " ";
        if(hh < 10)
            clock += "0";
        clock += hh + ":";
        if (mm < 10) clock += '0'; 
        clock += mm; 
        return(clock); 
    } </span>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
js大全 . 1.1 全是数字.. 3 1.2 限制 期限 不能大于 100 年.. 4 1.3 将日期格式由yyyy-MM-dd,转换为yyyymmdd,以直接比较两个日 期的大小.. 4 1.4 判断字符串是否全部由数字组成. 5 1.5 判断字符串是否全部由字母组成. 6 1.6 对于月和日,如果前面有零,去除,如 01 变为 1 .. 6 1.7 为 1 位的日月在前面补零,如将 1 变为 01 .. 7 1.8 在删除按钮的onclick事件触发.. 8 1.9 对输入控件的内容进行非空检验,如果为空,弹出提示信息.. 10 1.10 检验控件值是否是固定位数的有效数字 11 1.11 所输内容应为字母 12 1.12 过滤掉"'"、"%"、"&"、"/"、"" 13 1.13 删除字符串两侧的空格. 14 1.14 取字符串长度(不包含两端的空格) 14 1.15 清空查询条件.. 16 1.16 价格输入是否合法(99,999.00) 17 1.17 检验货票号为 1 位字母+6 位数字.. 17 1.18 检验起始货票号必须小于截至货票号. 18 1.19 检验查询条件是否为空,为空则提示"查询条件不能为空!" 19 1.20 检验是不是最短位数的数字,用于模糊查询. 20 1.21 输入为Decimal格式的数,且小数点前后都不能超过规定的位数 20 1.22 电话号码校验(只能为横线—、()、或数字) . 21 1.23 检测输入是否为正整数数字,如果结果为非给出相应提示. 22 1.24 避免多次提交页面 在jsp中嵌入该文件,在每个jsp页面的提交语23 1.25 检查EMail格式 23 1.26 电话号码由数字和-组成 24 1.26 检验输入的日期yyyy-MM-dd 27 1.27 去掉界面上开头的空格,过滤掉"'" 和""" . 33 1.29 去掉汉字间空格. 35 1.30 大小写转换 36 1.31 过滤掉"'"和""" . 36 1.32 清空查询条件.. 37 1.33 过滤掉"'"和"""和空格. 37 1.34 操作员代码为 7 位数字,密码最少为 4 位 .. 38 1.35 身份证为 15 或 18 位数字或字母 38 1.36 检验输入的时间段,日期格式yyyy-MM-dd 39 1.37 检验输入的时间段,日期格式yyyy-MM-dd 41 1.39 检查EMail格式是否正确.. 47 1.40 电话号码校验(只能为下划线—、()、或数字) 49 1.41 所输内容应为字母 50 1.41 所输内容不能非空 50 1.42 价格输入是否合法(99,999.00) 51 1.43 价格输入是否合法(99,999.00) 52 1.44 判断文本内容是否少于 2 个汉字/字符(js中汉字和字符占位一 样).. 53 1.45 检测输入是否只有字母或数字. 53 1.46 检测输入是否只有数字或不详,且输入数值不能过大(不能超过 32767).. 54 1.47 检测输入是否为数字.. 55 1.48 监测输入是否为数字,且所输入数值不能过大(不能超过 5 位 32767).. 56 1.49 检测输入是否为整形数字,且所输数值不能过大(不能超过 10 位 2147483647). 57 1.52 输入为Decimal格式的数,且小数点前后都不能超过规定的位数 /************************************ .. 59 1.53 可以为负数的数字,数值不能超过 5 位 32767 . 60 1.54 险情报告编号为四位年份加三位数字. 61 1.55 检验度数控制值是否是数字,并且小于或等于 180; /************************************ .. 62 1.56 删除字符串两侧的空格,返回的是删除两侧空格的字符串. 63 1.57 打开模式窗口,窗口居中 65 1.59 检验输入的时间段,日期格式yyyy-MM-dd 67 1.60 检验输入的日期yyyy-MM-dd 69 1.61 用下拉框显示月、日,日下拉框根据年月确定 73 1.62 检验租赁合同号为 4 位年份+2 位月份+本月合同的顺序号 2 位 75 1.63 用于多行校验,校验每一行,每一列值是否为空,传入的必须是 . 76 1.64 检查某列得多值中是否存在重复值.. 79 1.65 检查某列得多值与某一值得比较是否相同,例如多个到站路局与 一个发站路局是否相同 80 1.66 检查两多值列是否相同,例如同一行的装车路局是否与卸车站所 属路局是否相同 80 1.67 检查箱数与组数. 81 1.68 检查箱数与组数. 82 1.69 检查箱数与组数. 84 1.70 检查单列多值不能超过一定得长度.. 85 1.71 取字符串长度(不包含两端的空格 . 86 1.72 向表格中添加一行 86 1.73 将添加的输入框元素和Validator进行绑定。.. 89 1.74 根据标签展示的 的validator属性获取校验器类名(DecimalValidator) 91 1.75 删除一行. 92 1.76 根据箱型,校验箱数与组数的值是否符合规定 93 1.77 删除字符串两侧的空格. 95 1.78 检验单个集装箱箱号为 4 位字母+7 位数字. 95 1.79 检验多个集装箱箱号为 4 位字母+7 位数字. 97 1.80 检验单个集装箱箱号为'TBJU'+7 位数字用于租赁部分的箱号校验 .. 101 1.86 判断字符串是否全部由字母组成. 105 1.88 为 1 位的日月在前面补零,如将 1 变为 01 .. 107 1.90 检查EMail格式. 107 1.91 对输入控件的内容进行非空检验,如果为空,弹出提示信息. 109 1.92 检验控件值是否是固定位数的有效数字. 110 1.93 所输内容应为字母. 111 1.94 过滤掉"'"、"%"、"&"、"/"、"" . 112 1.95 取字符串长度(不包含两端的空格) . 113 1.96 将金额转化成按千分位的显示格式 114 1.97 清空查询条件 115 1.98 价格输入是否合法(99,999.00). 115 1.99 检验货票号为 1 位字母+6 位数字 116 2.00 检验起始货票号必须小于截至货票号.. 117 2.01 检验查询条件是否为空,为空则提示"查询条件不能为空!" . 118 2.02 检验是不是最短位数的数字,用于模糊查询.. 118 2.03 输入为Decimal格式的数,且小数点前后都不能超过规定的位数 .. 119 2.04 电话号码校验(只能为横线?、()、或数字) . 120 2.05(1)电话号码由数字和-组成 120 (2)电话号码为 7 到 13 位 . 120 (3)13 开头的头的手机号码必须为 11 位 120 2.06 检测输入是否为正整数数字,如果结果为非给出相应提示.. 121 2.07 避免多次提交页面 在jsp中嵌入该文件,在每个jsp页面的提交语句 前执行该方法.. 121 2.08 检查EMail格式. 122 2.09 电话号码由数字和-组成. 123 2.10 检验IP地址 . 124 2.11 校验忽略样式内容后文本域中的内容是否为空//校验忽略样式内容 后文本域中的内容是否为空.. 124 2.12 计算天数差的函数. 125 2.13 把结束日期月份加上月末日期.. 125 2.14 检验输入的日期yyyy-MM 126

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值