admin管理员组

文章数量:1201603

I am using the next code to convert a date received from a MySQL database format 1993-10-23 00:00:00 and display it in spanish:

alert(moment('1993-10-23 00:00:00', 'YYYY-MM-DD', 'es')); 

23 oct is saturday. I would expect to get sábado but I get the next:

Sat Oct 23 1993 00:00:00 GMT+0200

Also tried adding: moment.locale('es-ES'); , moment.locale('en-ES'); and moment.locale('es'); but neither works.

What's the correct way of converting dates from a language to another?

I am using the next code to convert a date received from a MySQL database format 1993-10-23 00:00:00 and display it in spanish:

alert(moment('1993-10-23 00:00:00', 'YYYY-MM-DD', 'es')); 

23 oct is saturday. I would expect to get sábado but I get the next:

Sat Oct 23 1993 00:00:00 GMT+0200

Also tried adding: moment.locale('es-ES'); , moment.locale('en-ES'); and moment.locale('es'); but neither works.

What's the correct way of converting dates from a language to another?

Share Improve this question edited Jun 15, 2015 at 10:13 Alpha2k asked Jun 15, 2015 at 10:08 Alpha2kAlpha2k 2,2417 gold badges43 silver badges67 bronze badges 3
  • 1 You're using the syntax for parsing a string, not outputting it. You want moment().format(). Be careful with ISO 8601 like formats without a timezone, they are treated differently by ES5 (UTC) and ES6 (local). – RobG Commented Jun 15, 2015 at 10:23
  • @RobG thanks, check the answer, is there anything to add for the ISO format ? – Alpha2k Commented Jun 15, 2015 at 10:32
  • ISO 8601 says that dates with a missing timezone should be treated as local, whereas ES5 says to treat them as UTC. ES6 changes to be consistent with ISO. To be sure, you should use '1993-10-23T00:00:00Z' if you want it treated as UTC. Add the required timezone if you want something else and parse it with Moment.js (or write your own 2 or 3 line function). The Javascript parse will only parse ISO 8601 UTC. – RobG Commented Jun 15, 2015 at 10:56
Add a comment  | 

2 Answers 2

Reset to default 17

This seems to work, thanks @RobG

var localLocale = moment('1993-10-23 00:00:00');
moment.locale('es');
localLocale.locale(false);
alert(localLocale.format('LLLL')); 

The following method worked for me

moment(agreement.dateStart).locale('es').format('LLLL')

本文标签: javascriptMomentJS time in spanishStack Overflow