JS日期加減,日期運算

來源:互聯網
上載者:User

標籤:http   io   ar   java   for   sp   div   on   art   

一、日期減去天數等於第二個日期

function cc(dd,dadd)
{
//可以加上錯誤處理
var a = new Date(dd)
a = a.valueOf()
a = a - dadd * 24 * 60 * 60 * 1000
a = new Date(a)
alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")
}
cc("12/23/2002",2)

這裡不得不做補充,浪費好多時間得出教訓:
Javascript 對時間的代號
0-11數字表示1-12月: var a= new Date(2006,5,6) 結果是2006-6-6
0-6表示星期
1-31表示日期
0-23小時
0-59分鐘,秒

二。//兩個日期的差值(d1 - d2).
function DateDiff(d1,d2){
    var day = 24 * 60 * 60 *1000;
try{    
        var dateArr = d1.split("-");
   var checkDate = new Date();
        checkDate.setFullYear(dateArr[0], dateArr[1]-1, dateArr[2]);
   var checkTime = checkDate.getTime();
  
   var dateArr2 = d2.split("-");
   var checkDate2 = new Date();
        checkDate2.setFullYear(dateArr2[0], dateArr2[1]-1, dateArr2[2]);
   var checkTime2 = checkDate2.getTime();
    
   var cha = (checkTime - checkTime2)/day;  
        return cha;
    }catch(e){
   return false;
}
}//end fun

 

三、應用:
<script language="javascript" type="text/javascript">
var flag = Array();//全域變數

var start = "2009-01-01";
var end = "2009-02-24";
SetFlag(start,end);

for(var i=0; i<flag.length; i++){
   document.write(flag[i]+"\n\r");
}

//設定周期內的日期(數組)
function SetFlag(start,end){
var cdate = Array();
cdate = start.split("-");
var cd = cdate[1]+"/"+cdate[2]+"/"+cdate[0];
var dayNum = DateDiff(end,start);
for(var i=0; i<=dayNum; i++){
   flag.push(AddDays(cd,i));
}
}//end fun

//日期加上天數後的新日期.
function AddDays(date,days){
var nd = new Date(date);
   nd = nd.valueOf();
   nd = nd + days * 24 * 60 * 60 * 1000;
   nd = new Date(nd);
   //alert(nd.getFullYear() + "年" + (nd.getMonth() + 1) + "月" + nd.getDate() + "日");
var y = nd.getFullYear();
var m = nd.getMonth()+1;
var d = nd.getDate();
if(m <= 9) m = "0"+m;
if(d <= 9) d = "0"+d;
var cdate = y+"-"+m+"-"+d;
return cdate;
}
//兩個日期的差值(d1 - d2).
function DateDiff(d1,d2){
    var day = 24 * 60 * 60 *1000;
try{    
        var dateArr = d1.split("-");
   var checkDate = new Date();
        checkDate.setFullYear(dateArr[0], dateArr[1]-1, dateArr[2]);
   var checkTime = checkDate.getTime();
  
   var dateArr2 = d2.split("-");
   var checkDate2 = new Date();
        checkDate2.setFullYear(dateArr2[0], dateArr2[1]-1, dateArr2[2]);
   var checkTime2 = checkDate2.getTime();
    
   var cha = (checkTime - checkTime2)/day;  
        return cha;
    }catch(e){
   return false;
}
}//end fun
</script>

JS日期加減,日期運算

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.