7
我想比較javascript中的ISO 8601日期作爲字符串,而不是爲每個字符串和比較對象創建Date對象。在JavaScript中比較ISO 8601日期字符串
var date_array = ['2012-10-01','2012-11-27','2012-12-23'];
console.log(date_array[0] < date_array[1]) // gives true
console.log(date_array[1] > date_array[2]) // gives false
我這樣做的原因是我認爲比較字符串應該比使物體每個日期字符串和對象比較快。
這些比較似乎在某些瀏覽器中按預期工作。我可以期待這種按字母順序排列的
lexicographic字符串比較可以在所有瀏覽器中工作嗎?這種日期比較方法實際上比使用Date對象更快嗎?
如果格式來自大單元 - >小單元,並且所有字符串對於每個字段具有相同的長度(必要時填寫前導0),則應該沒有問題。 – nhahtdh
相關鏈接:[Here](http://jsperf.com/date-object-creation/2)顯示使用_Date_構造函數的操作數/秒,[here](http://jsperf.com/operator-vs -localecompage/3)是字符串比較和[這裏](http://jsperf.com/inequality-str-vs-int)int比較。 –
根據[維基百科](https://en.wikipedia.org/wiki/ISO_8601#General_principles),ISO 8601日期的組成部分按字典順序排列,因此您應該很好地執行上述操作 – Dogoku