Search code examples
javascriptdatedatetimemomentjsdateformatter

Display date in English and Japanese using moment.js or using JQuery/Javascript


I am using moment.js for formatting dates. I want to display date in 'en-JP' culture, but moment.js does not support 'en-JP' language.

Jquery/Javascript code would also be fine.

I want to display date like:

2018年 Aug月 15日 Wednesday


Solution

  • It seems "年", "月", "日" are Japanese for "year", "month", "day" so don't change with the date. So you don't need a library if there's reasonable support for the Intl object via toLocaleString, e.g.

    var d = new Date();
    var lang = 'en',
        year = d.toLocaleString(lang, {year:'numeric'}),
        month = d.toLocaleString(lang, {month:'short'}),
        day = d.toLocaleString(lang, {day:'numeric'}),
        dayName = d.toLocaleString(lang, {weekday:'long'});
    
    console.log(
      `${year}年 ${month}月 ${day}日 ${dayName}`
    );