开发者

How to convert Localized Date to standard Date in 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"...

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

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.


You will need to convert your input data into dd, mm, yyyy numeric format then give those to the Date constructor. Note that mm is zero relative.

In your specific example, a regExp of /(/d{1,2}) de ([a-z]+) de (/d{4})/ would provide the day and year as numbers, given an array of month names, indexOf would provide the month number.

Each language would need a specific month table an/or reg exp.


I would make a reverse mapping of locale-specific month names to number (and day names, too, probably) and a list of common date formats. Then you can check each string against those formats and, when matched, parse the values (e.g. with a regular expression) and construct a JavaScript Date using the extracted data.

Here is some pseudo code to demonstrate the idea (just for illustration, not intended to work):

var i18nDate = {
  'es': {
    'days': ['lunes', 'martes', 'miercoles', ...],
    'months': ['enero', 'febrero', 'marzo', ...],
  },
  // ... for each locale.
};

var i18nFormat = {
  'es': [
    new RegExp( // Format 1, e.g. "lunes, 29 de agosto de 2011"
      '(' + i18nDate.es.days.join('|') + ')' + ', de (\d+) '
      + '(' + i18nDate.es.months.join('|') + ')' + ' de (\d+)'),
    // ... for each common date format.
  ],
  // ... for each locale.
};

function parseI18nDate(s) {
  var locale, formats, i, m;
  for (locale in i18nFormat) {
    formats = i18nFormat[locale];
    for (i=0; i<formats.length; i++) {
      if (m = s.match(formats[i])) {
        return new Date(m[x], m[y], m[z]);
        // TODO: mapping of field position to date component.
      }
    }
  }
  return null;
}

Obviously there are a lot of details to be worked out including the locale specific formats and values. The effort is large enough to warrant a full-fledged library. Perhaps there is already an open source project to do such a thing?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜