如何在JavaScript中将本地化日期转换为标准日期? [英] How to convert Localized Date to standard Date in JavaScript?

查看:156
本文介绍了如何在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.

这篇关于如何在JavaScript中将本地化日期转换为标准日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆