Search code examples
javascriptdatelocalemomentjs

Converting a date from one language to another


I have a date in Thai as:

31มี.ค.2017, 16:56:51

which translates to

Mar 31, 2017, 16:56:51

using google translate. Is there a way, I could do this using momentjs locale support? If not, is there any other way out?


Solution

  • Momentjs suports i18n and supports Thai [th] locale, you can parse your input using moment(String, String, String).

    Then you can use locale() and format() to show the value using english locale.

    Here a working sample:

    var m = moment('31มี.ค.2017, 16:56:51', 'DDMMMMYYYY, HH:mm:ss', 'th');
    console.log(m.locale('en').format('MMM DD, YYYY, HH:mm:ss')); // Mar 31, 2017, 16:56:51
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>