2012-04-19 77 views
6

我想獲取擴大日期去特定的日期,所以我想用今天的日期來檢測特定的日期。但是這不是在這裏工作是我的code如果日期是下個月27我怎樣才能得到剩下的日子去我如何找到使用Javascript的兩個日期之間的差異

var date2=new Date(); 
    var date1=27/5/2012; 
    var diff = date1.getDate()-date2.getDate(); 
    var date_reaming = diff.getDate(); 
    document.write(date_reaming + 'days to go'); 
+0

的可能重複:HTTP://計算器。 COM /問題/ 542938 /怎麼辦-I-GET-的用戶號碼的天之間,兩日合的jQuery – billyonecan 2012-04-19 09:34:44

回答

3

下面是答案,我發現這個從here我的JS-小提琴是here

var d = new Date(); 
var curr_date = d.getDate(); 
var curr_month = d.getMonth();/* Returns the month (from 0-11) */ 
var curr_month_plus= curr_month+1; /* because if the month is 4 it will show output 3 so we have to add +1 with month*/ 
var curr_year = d.getFullYear(); 
function dstrToUTC(ds) { 
    var dsarr = ds.split("/"); 
    var mm = parseInt(dsarr[0],10); 
    var dd = parseInt(dsarr[1],10); 
    var yy = parseInt(dsarr[2],10); 
    return Date.UTC(yy,mm-1,dd,0,0,0); } 
    function datediff(ds1,ds2) { 
    var d1 = dstrToUTC(ds1); 
    var d2 = dstrToUTC(ds2); 
    var oneday = 86400000; 
    return (d2-d1)/oneday; } 
    var a =curr_month_plus+ '/' + curr_date + '/' + curr_year; 
    var b; 
    b = "5/26/2012"; 
    document.write(+datediff(a,b)+" day(s)<br>"); 
14

您的代碼

date1=27/5/2012 

,其實就是27除以5,到2012年它相當於分爲寫作

date1 = 0.0026838966202783303 

DATE1將是一個數字,這個數字有沒有getDate方法。

如果你宣佈他們爲實際的日期對象,而不是

var date2 = new Date(2012, 3, 19); 
var date1 = new Date(2012, 4, 27); 

您將能夠執行

var diff = date1 - date2; 

這將使你在這兩個日期之間的毫秒的差異。

從這裏,你可以計算天數,像這樣:

var days = diff/1000/60/60/24; 
8
function getDateDiff(date1, date2, interval) { 
    var second = 1000, 
    minute = second * 60, 
    hour = minute * 60, 
    day = hour * 24, 
    week = day * 7; 
    date1 = new Date(date1).getTime(); 
    date2 = (date2 == 'now') ? new Date().getTime() : new Date(date2).getTime(); 
    var timediff = date2 - date1; 
    if (isNaN(timediff)) return NaN; 
    switch (interval) { 
    case "years": 
     return date2.getFullYear() - date1.getFullYear(); 
    case "months": 
     return ((date2.getFullYear() * 12 + date2.getMonth()) - (date1.getFullYear() * 12 + date1.getMonth())); 
    case "weeks": 
     return Math.floor(timediff/week); 
    case "days": 
     return Math.floor(timediff/day); 
    case "hours": 
     return Math.floor(timediff/hour); 
    case "minutes": 
     return Math.floor(timediff/minute); 
    case "seconds": 
     return Math.floor(timediff/second); 
    default: 
     return undefined; 
    } 
} 

console.log(getDateDiff('19/04/2012', '27/5/2012', 'days')); 
2

請了解jQuery是用於製造。

jQuery是一個快速,簡潔的JavaScript庫,簡化HTML文檔遍歷事件處理動畫,並Ajax交互快速Web開發。

你想用基本 Javacript或胡安G.烏爾塔多國家如momentjs另一個庫。

3

我想你可以減去它:

var date2 = new Date(2012, 3, 19); // 1st argument = year, 2nd = month - 1 (because getMonth() return 0-11 not 1-12), 3rd = date 
var date1 = new Date(2012, 4, 27); 
var distance = date1.getTime() - date2.getTime(); 
distance = Math.ceil(distance/1000/60/60/24); // convert milliseconds to days. ceil to round up. 
document.write(distance); 
相關問題