Date format - momentjs - datetime excerpt using locale language

I am using library.js library I have a date in this format:

2014-08-07T10:00:00+02:00

      

I want to have two separate values:

- Thursday, August 7 2014
- 10 am

      

But I also want them to use the local language. For example, if time.lang ("fr"), the output should be

- Jeudi 7 Aoรปt 2014
- 10h

      

I have installed moment.js lang correctly. I was able to remove the hour, minutes and seconds (to extract the first value):

new Date(moment.utc(date).format('LL')) //Outputs Thu Aug 07 2014 00:00:00 GMT+0200 (Paris, Madrid)

      

But I don't know how to extract the hour and minutes (for the second value) and how to display the date using the current language .

+3


source to share


4 answers


@Rayjax's solution no longer works in recent versions of moment.js. Changed behavior moment.localeData().longDateFormat()

. LT

has now been replaced by the time format. Instead, dddd, MMMM D, YYYY LT

it returns now dddd, MMMM D, YYYY h:mm A

. Therefore, deleting LT

from a string no longer works. But we could just remove the one compiled LT

for the current locale:

moment.localeData().longDateFormat('LLLL')
.replace(
  moment.localeData().longDateFormat('LT'), '')
.trim();

      



Cropping is necessary to avoid unnecessary spaces.

+1


source


Hi i dont know if this is the best way to do it but it works

moment.locale("en");

var now = moment()

console.log(now.format(moment.localeData().longDateFormat('LLLL').replace('LT' , '')));

console.log(now.format(moment.localeData().longDateFormat('LT').replace('mm' , '').replace(':' , '').replace('.' , '')))

      



http://jsfiddle.net/yann86/tb0u5eav/

0


source


I came up with what works well:

moment.lang("fr"); //en, es or whatever
var date = moment(dateParam).format("LL"), 
time = moment(dateParam).format("LT");
console.log(date, time) -- outputs separatedly date and time in the correct language form

      

What I was missing was in the language config files: http://momentjs.com/ I grabbed momentjs + locales instead of instant.js and it worked.

It is important to note that moment.js will not tell you that you missed the ones that were filed, it will be the default by default.

0


source


It worked for my needs:

moment().locale('en').format('L');
moment().locale('pt-br').format('L');

      

Just change the format to suit your needs. The moment documentation is great. http://momentjs.com/

0


source







All Articles