2013-04-08 24 views
2

我使用date.js將條件格式應用於數據網格。數據從javascript數組中解析。我所有的條件都正常工作,除了這一個:如何正確比較MM-dd-yyyy字符串中的年份?

如果(VAL <今天& &值> 01 -01-2000' )

VAL在MM-DD-YYYY格式的字符串我無法改變。所以我使用date.js將今天的日期轉換爲MM-dd-yyyy格式的字符串並進行比較。問題在於,2014年1月17日被視爲比2013年4月4日少 - 因爲它正在比較字符串。

最好的解決方法是什麼?

我想簡化一下,這就是爲什麼我首先轉換爲字符串的原因,但我不確定如何解決年度問題。

感謝您的幫助!

var today = new Date.today().toString("MM-dd-yyyy"); 
var tomorrow = new Date.today().addDays(1).toString("MM-dd-yyyy"); 
var upcoming = new Date.today().addDays(7).toString("MM-dd-yyyy"); 

function eXcell_edncl(cell) { 
    this.base = eXcell_edn; 
    this.base(cell); 
    this.setValue = function(val) { 
     if (val.indexOf('ACT') >= 0) this.cell.style.backgroundColor="lightgreen"; 
     else if (val.indexOf('PV') >= 0) this.cell.style.backgroundColor="lightgreen", this.cell.style.fontSize="20px"; 
     else if (val.indexOf('YES') >= 0) this.cell.style.backgroundColor="lightgreen", this.cell.style.fontSize="20px"; 
     else if (val < today && val > '01-01-2000') this.cell.style.backgroundColor="red"; 
     else if (val == today) this.cell.style.backgroundColor="orange"; 
     else if (val == tomorrow) this.cell.style.backgroundColor="yellow"; 
     else if (val > tomorrow && val <= upcoming) this.cell.style.backgroundColor="lightyellow"; 
     else this.cell.style.backgroundColor=""; 
     this.cell.innerHTML = this.grid._aplNF(val, this.cell._cellIndex); 
    } 
} 

回答

1

解決此問題的最佳方法是不將Date對象轉換爲字符串。 "01-17-2014" < "04-08-2013"的計算結果爲true,因爲"01" < "04"爲真,因此無論粘貼到這些字符串上總會以相同的方式進行評估。但是,在Date對象上使用少於/大於運算符的行爲將按預期運行。所以,你可以修改現有的if語句是

if (new Date(val) < new Date(today) && new Date(val) > new Date('01-01-2000')) 

,這將解決您的問題,但你可能會更好過使用Date對象開始。

+0

謝謝。這就說得通了。我試過: else if(new Date(val) new Date('01 -01-2000'))this.cell.style.backgroundColor =「red」; 但是,它不會將今天的日期確定爲紅色背景。 – user1911141 2013-04-08 19:07:24

+0

比較''日期''這樣的實例,因爲[關係運算符'<' and '>'](http://www.ecma-international.org/ecma-262/5.1/#sec-11.8.1)隱式調用實例的'valueOf ()方法(它是從Date.prototype繼承的),通過內部的ToPrimitive算法返回實例的時間值(和getTime()一樣)。它可以用於擁有或繼承這種方法的任何其他對象。 – PointedEars 2013-04-08 19:16:48

+0

@ user1911141'新日期('01 -01-2000')'不會在2000年1月1日生成日期實例。您希望[RTFM支持日期格式](https://developer.mozilla。 org/en-US/docs/JavaScript/Reference/Global_Objects/Date),或者編寫'New Date(2000,0,1)'確定(是的,該月是基於0的)。 – PointedEars 2013-04-08 19:19:21

1

由於您使用date.js你可以使用它的比較功能,如描述的documentation

Date.compare(日期DATE1,DATE2日期):數

比較第一次約會到第二個日期並返回一個數字表示它們的相對值。 -1 =這比日期少。 0 =值相等。 1 =這比日期更重要。

請參閱代碼示例文檔。

1

您可能無法更改用戶界面中日期字符串的格式,但不管它在後端的格式如何。您應該更改代碼以使用ISO 8601,該代碼旨在允許在字符串格式中輕鬆進行比較(以及其他優點)。

然後,您的日期格式爲yyyy-MM-dd,這將允許您將它們作爲字符串進行比較。

並且因爲它是相關的,請檢查出this XKCD comic如果您仍在圍欄上。

+0

謝謝傑西。這對我最有意義,但我不知道如何將我的val字符串從MM-dd-yyyy轉換爲yyyy-MM-dd。我嘗試過: var val2 = new Date.val.toString(「yyyy-MM-dd」); 和 var val2 = new Date(val).toString(「yyyy-MM-dd」); ,但都沒有伎倆。你能幫我解決這個問題嗎? – user1911141 2013-04-08 19:41:11

+0

@ user1911141我不是JavaScript的專家,尤其是JavaScript中的日期。但[此StackOverflow問答](http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript)可能會幫助你。如果您仍然有問題,請隨時爲您的具體問題創建一個新問題,正確的人會盡力幫助您。 – 2013-04-08 20:12:22

0

完成以下工作。需要分析值進來:

var today = new Date.today().toString("MM-dd-yyyy"); 
var today2 = new Date.today(); 
var old = new Date(2000, 0, 1); 
var tomorrow = new Date.today().addDays(1).toString("MM-dd-yyyy"); 
var upcoming = new Date.today().addDays(7).toString("MM-dd-yyyy"); 

function eXcell_edncl(cell) { 
    this.base = eXcell_edn; 
    this.base(cell); 
    this.setValue = function(val) { 
     var val2 = new Date.parse(val); 
     if (val.indexOf('ACT') >= 0) this.cell.style.backgroundColor="lightgreen"; 
     else if (val.indexOf('PV') >= 0) this.cell.style.backgroundColor="lightgreen", this.cell.style.fontSize="20px"; 
     else if (val.indexOf('YES') >= 0) this.cell.style.backgroundColor="lightgreen", this.cell.style.fontSize="20px"; 
     else if (val2 < today2 && val2 > old) this.cell.style.backgroundColor="red"; 
     else if (val == today) this.cell.style.backgroundColor="orange"; 
     else if (val == tomorrow) this.cell.style.backgroundColor="yellow"; 
     else if (val > tomorrow && val <= upcoming) this.cell.style.backgroundColor="lightyellow"; 
     else this.cell.style.backgroundColor=""; 
     this.cell.innerHTML = this.grid._aplNF(val, this.cell._cellIndex); 
    } 
} 
+0

默認情況下,您的「最佳」選項是使用日期對象,只能轉換爲字符串輸出。 – RobG 2013-04-09 04:13:54