我使用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);
}
}
謝謝。這就說得通了。我試過: else if(new Date(val) new Date('01 -01-2000'))this.cell.style.backgroundColor =「red」; 但是,它不會將今天的日期確定爲紅色背景。 –
user1911141
2013-04-08 19:07:24
比較''日期''這樣的實例,因爲[關係運算符'<' 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
@ 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