2015-06-19 46 views
-1

我需要開發一個代碼,將計算的日期格式:計算上的日期 - 記錄日期中的相對

上個星期天 - 上週四 - 週日的最後一個實例之前。 (當前日10-06)07-06,04-06,31-05

上週四 - 上週日 - 倒數第二個週四例如。 (當前日05-06)04-06,31-05,28-05

如果當天是星期四:當前星期四 - 上週日 - 上週四例如。 (當前日18-06)18-06,14-06,11-06

如果當天是星期天:當前星期日 - 上週四 - 上週日例如。 (當前21-06)21-06,18-06,14-06

只是有時壞計算這些日期顯示不應該是,因爲它是最後14-06,它顯示的第一個日期:14 -06,第二次14-06,第三次11-06的日期不好。今天顯示(19-06):14-06,11-06,07-06應該18-06,14-06,11-06。

function getLastThursday() { 
    var lt = new Date(); 
    lt.setDate(lt.getDate() - lt.getDay()-3); 
    // lt = lt.getDate(); 
    return lt; 
} 
function getLastSunday() { 
    var ls = new Date(); 
    ls.setDate(ls.getDate() - ls.getDay()); 
    // ls = ls.getDate(); 
    return ls; 
} 
//penultimate Thursday 
function get2LastThursday() { 
    var twolt = new Date(); 
    twolt.setDate(twolt.getDate() - twolt.getDay()-10); 
    // lt = lt.getDate(); 
    return twolt; 
} 
// penultimate Saturday 
function get2LastSunday() { 
    var twols = new Date(); 
    twols.setDate(twols.getDate() - twols.getDay()-7); 
    // ls = ls.getDate(); 
    return twols; 
} 
var dLastThursday = getLastThursday().getDate();//number of the day 
var dLastSunday = getLastSunday().getDate();//number of the day 
var d2LastThursday = get2LastThursday().getDate();//number penultimate day 
var d2LastSunday = get2LastSunday().getDate();//number penultimate day 
var mLastThursday = getLastThursday().getMonth()+1; // month number 
var mLastSunday = getLastSunday().getMonth()+1; // month number 
var m2LastThursday = get2LastThursday().getMonth()+1; // number penultimate month 
var m2LastSunday = get2LastSunday().getMonth()+1; // number penultimate month 
var dForAllLastThursday = ('0' + (getLastThursday().getDate())).slice(-2);//to display from zero 
var mForAllLastThursday = ('0' + (getLastThursday().getMonth()+1)).slice(-2); 
var dForAllLastSunday = ('0' + (getLastSunday().getDate())).slice(-2); 
var mForAllLastSunday = ('0' + (getLastSunday().getMonth()+1)).slice(-2); 
var d2ForAllLastSunday = ('0' + (get2LastSunday().getDate())).slice(-2); 
var m2ForAllLastSunday = ('0' + (get2LastSunday().getMonth()+1)).slice(-2); 
var d2ForAllLastThursday = ('0' + (get2LastThursday().getDate())).slice(-2); 
var m2ForAllLastThursday = ('0' + (get2LastThursday().getMonth()+1)).slice(-2); 
var allLastThursday = dForAllLastThursday+"-"+mForAllLastThursday; // Day - month 
var allLastSunday = dForAllLastSunday+"-"+mForAllLastSunday; // day - month 
var all2LastThursday = d2ForAllLastThursday+"-"+m2ForAllLastThursday; // day - penultimate month 
var all2LastSunday = d2ForAllLastSunday+"-"+m2ForAllLastSunday; // day - penultimate month 

var roznica = dLastSunday - dLastThursday; 

var d = new Date(); 
if(d.getDay() == 4 || d.getDay() == 0){ 
    var isToday = 1; // confirmation that today is Thursday or Sunday 
    var dToday = d.getDate(); 
    var mToday = d.getMonth()+1; 
    //adding a zero to the front as you need when you 1-9 
    var dForAllToday = ('0' + d.getDate()).slice(-2); 
    var mForAllToday = ('0' + (d.getMonth()+1)).slice(-2); 
    var allToday = dForAllToday+"-"+mForAllToday; 
//the problem with bad showing how the first Thursday or Sunday of the month and last Thursday or Sunday of the month to reduce the problem  
    if (roznica > 20 || roznica < -20) { 
     if(dLastSday < dLastFday) 
     { 
      lastday = allLastSunday; 
      lastday2 = allLastThursday; 
     } 
     else 
     { 
      lastday = allLastThursday; 
      lastday2 = allLastSunday; 
     } 
    }else if(dLastSunday > dLastThursday){ 
     lastday = allLastSunday; 
     lastday2 = allLastThursday; 
    }else{ 
     lastday = allLastThursday; 
     lastday2 = allLastSunday; 
    } 
} 
//the problem with bad showing how the first Thursday or Sunday of the month and last Thursday or Sunday of the month to reduce the problem 
else if (roznica > 20 || roznica < -20) 
{ 
    if(dLastSunday < dLastThursday) 
    { 
     lastday = allLastSunday; 
     lastday2 = allLastThursday; 
     lastday3 = all2LastSunday; 
    } 
    else{ 
     lastday = allLastThursday; 
     lastday2 = allLastSunday; 
     lastday3 = all2LastThursday; 
    } 
} 
else if(dLastSunday > dLastThursday) 
{ 
    lastday = allLastSunday; 
    lastday2 = allLastThursday; 
    lastday3 = all2LastSunday; 

} 
else 
{ 
    lastday = allLastThursday; 
    lastday2 = allLastSunday; 
    lastday3 = all2LastThursday; 
} 
if (typeof allToday != 'undefined') { 
    firstDay = allToday; 
    secondDay = lastday; 
    thridDay = lastday2; 
} else{ 
    firstDay = lastday; 
    secondDay = lastday2; 
    thridDay = lastday3; 
} 
console.log(allLastSunday); 
console.log(allLastThursday); 
console.log(getLastSunday()); 
console.log(getLastThursday()); 
var piecesFirst = firstDay.split("-"); 
var firstCharPiecesFirst = piecesFirst[0].charAt(0); 
var firstCharPiecesFirst2 = piecesFirst[1].charAt(0); 
if (firstCharPiecesFirst=="0") {piecesFirst[0] = piecesFirst[0].charAt(1);}; 
if (firstCharPiecesFirst2=="0") {piecesFirst[1] = piecesFirst[1].charAt(1);}; 
var piecesSecond = secondDay.split("-"); 
var firstCharPiecesSecond = piecesSecond[0].charAt(0); 
var firstCharPiecesSecond2 = piecesSecond[1].charAt(0); 
if (firstCharPiecesSecond=="0") {piecesSecond[0] = piecesSecond[0].charAt(1);}; 
if (firstCharPiecesSecond2=="0") {piecesSecond[1] = piecesSecond[1].charAt(1);}; 
var piecesThrid = thridDay.split("-"); 
var firstCharPiecesThrid = piecesThrid[0].charAt(0); 
var firstCharPiecesThrid2 = piecesThrid[1].charAt(0); 
if (firstCharPiecesThrid=="0") {piecesThrid[0] = piecesThrid[0].charAt(1);}; 
if (firstCharPiecesThrid2=="0") {piecesThrid[1] = piecesThrid[1].charAt(1);}; 
+0

它在「分解」時究竟做了什麼?給出具體細節,否則你在這裏得不到多少幫助。 – forgivenson

+0

您可能希望查看像moment.js這樣的庫,而不是重新發明輪子。 –

+0

我知道在函數getLastThursday(),get2LastThursday(),get2LastSunday()的問題,但我不知道如何得到上週四,2LastThursday,2LastSunday – Sliwka92

回答

0

我用的getTime()的日期計算,只是加/減毫秒。 這有額外的好處,你可以自動處理像閏年這樣的事情,並且可以更容易地檢查諸如DST之類的事情。

+0

DST是什麼? – Sliwka92

+0

夏令時。 – Shilly