javascript - 如何将日期字符串中的外国月份转换为英文

标签 javascript globalization datetime-format

下面对另一个线程的回答似乎开始了我认为我需要的事情,但我在实现它时遇到了困难。

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" );

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.

如果有人可以将其变成一个可行的示例,我就可以采用它并根据我的需要进行调整。

但是......有人知道替代方法吗?

例如我的字符串是:

Set 24, 2012 20:40:20”,其中包含罗马尼亚语月份。

我需要它

Sep 24, 2012 20:40:20”,其中有英文月份。

我需要一种解决方案,可以获取语言代码(即 en、es、pl、ru、ro 等)并转换字符串中的月份(需要能够处理所有 12 个月,而不仅仅是上面的字符串)从外语到英语。

任何快速的帮助将不胜感激,以便我可以让我的网站恢复并再次全速运行。

如果您需要我提供任何进一步的信息,请询问:)

问候 罗斯

@Jukka K.Korpela

我有这个:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing Globalize</title>
<meta name="keywords" content="Globalize, Testing">
<meta name="description" content="Trying to get globalize to work.">
<script src="https://github.com/jquery/globalize/blob/master/lib/globalize.js"></script>
<script src="https://github.com/jquery/globalize/blob/master/lib/cultures/globalize.cultures.js"></script>
<script>
var monthString = 'sep'; // replace by code that extracts the string
var lang = 'ro';          // replace by code that picks up the right language
var month = Globalize.parseDate(monthString, 'MMM', lang);
if(month) {
  document.write(Globalize.format(month, 'MMM', 'en'));
} else {
  alert('Unrecognized month: ' + monthString);
}
</script>
</head>

但是当我在“http://htmledit.squarefree.com/”中测试时它不起作用,请帮忙:)

最佳答案

我认为你可以使用 datejs 来达到你的目的。 它能够通过文化文件解析任何可用语言(超过 150 种国家语言)的日期数据,然后将其保存为英语或您想要的任何其他语言。

看看:http://code.google.com/p/datejs/

关于javascript - 如何将日期字符串中的外国月份转换为英文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12591985/

相关文章:

javascript - 检测 HTML5 视频标签

javascript - 设置选项后谷歌地图多边形不更新颜色?

c# - 如何从 CultureInfo 获取国家代码?

asp.net-mvc - 将客户端验证设置为德语

java - 在 Java 中将 Epoch 秒转换为日期和时间格式

javascript - 有什么方法可以根据外部值来依赖对象中键的存在吗?

javascript - 使用face-api.js包(node.js)时导入@tensorflow/tfjs-node时出现问题

javascript - datejs 与 Jquery/全局化

python-2.7 - 在python中将格式为dd.mm.yy的日期转换为yyyy-mm-dd

java - 为什么时区模式 "OOOO"不显示完整的 GMT+00 :00 offset format?