您的位置:首页 > Web前端 > JavaScript

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

2014-12-08 13:09 477 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: