2

I'm trying to use moment-with-locales.min.js to add (or subtract) days to a locale specific date. What I have are textboxes which contain dates formatted to specific locales. When the user updates one, it resets all of the others adding a specified number of days. However, I can't seem to get moment.js to manipulate the date based on anything other than en-US, so the dates are way off.

I've tried several variations using this jsFiddle

var d = new moment('11/08/2014').locale('en-GB'); //August 11, 2014
alert(d.locale()); //returns en-gb

d.add("days",1).format('L');
//d.add("days",1).locale('en-GB').format('L');
alert(d); //returns November 09, 2014 instead of 12/08/2014

//note, the format is incorrect as well, should be returning short format

I'm certain I'm using it incorrectly, so any advice would be welcome.

Metallicraft
  • 2,211
  • 5
  • 31
  • 52
  • `new moment('11/08/2014').locale('en-GB');` is parsed as `Sun Nov 09 2014 00:00:00 GMT+0100 (W. Europe Standard Time)` not Aug 11, 2004. So it's reading `mm/dd/yyyy` instead of `dd/mm/yyyy`. – Halcyon Aug 11 '14 at 15:00
  • @Halcyon yes, but afaik en-GB has date in format dd/MM/YYYY so it should parse it correctly.. – FrEaKmAn Aug 11 '14 at 15:04
  • Yes, that is what I would expect as well. Maybe the `.locale` call comes too late? I'm not familiar enough with momentjs. – Halcyon Aug 11 '14 at 15:07
  • Thank you both for your responses. – Metallicraft Aug 11 '14 at 15:15

1 Answers1

3

Based on the https://github.com/moment/moment/issues/665, you need to define L token for parser. At the same time, define locale before you start parsing. So the final code is

moment.locale('en-gb');
var d = new moment('11/08/2014', 'L'); //August 11, 2014
alert(d.locale()); //returns en-gb

d.add("days", 1);
alert(d.format('L')); //returns 12/8/2014

http://jsfiddle.net/omnbgk5s/2

FrEaKmAn
  • 1,785
  • 1
  • 21
  • 47