如何在JavaScript中将本地化日期转换为标准日期?

问题描述:

我正在撰写一段JavaScript代码来排序包含日期(包含本地化日期)和其他字段的数据表。例如:

I am writing a piece of javascript code to sort a table of data which contains a date (contains localized dates) and other fields. For eg:

"lunes, 29 de agosto de 2011", "field1", "field2"
"lunes, 28 de agosto de 2011", "field1", "field2"...

日期,我需要通过将日期转换成标准的Javascript日期对象进行比较。有没有办法将本地化日期转换为标准日期?

while sorting by date, I need to compare the dates by converting them into standard Javascript date objects. Is there any way to convert a localized date into standard date?

为此目的的最佳库可能是全球化。它允许您指定区域设置(它被称为文化,但实际上是相同的),格式(内置的或您自己的)并且实际上将字符串解析为给定日期:

The best library for that purpose would probably be Globalize. It allows you to specify the locale (it is called culture but it is actually the same), the format (built-in or your own) and actually parse string to given date:

var dateString = "lunes, 29 de agosto de 2011";
// seems like long date format in Spanish
var date = Globalize.parseDate( dateString, "D", "es" );

您需要附加适当的文化档案以及参考全球化才能使其发挥作用。请注意,图书馆已经定义了文化,所以不要害怕网页上的信息,您实际上不需要 .Net。

You would need to attach appropriate culture file as well as reference to Globalize to make it work. Please mind that cultures are already defined in the library, so don't be afraid of the message on the web page, you actually don't need .Net.