Posted by: R Manimaran | April 23, 2015

Moment Js: Change Locale in Date Time from SharePoint 2013 REST API

In the previous article I have explained about using Handlebars template and SharePoint data. For formatting the Date Time I have used Moment.js file. It has lot of helper functions and formatting options to work with Date Time.

When displaying the data time, Moment.js provides an option to change the locale. By default it will print in English. Locale details is available in the separate script file Moment.locale.js.

Include Moment.js and Moment.locale.js to project.

NugetMoment

The above command will install the following files in your project’s script folder.

  • Moment-with-locales.js
  • Moment-with-locales.min.js
  • Moment.js
  • Moment.min.js

To do it offline, download the above files from Momentjs.com and include to your project.

In the previous example I have wrote a Handlebar Helper method where i have used the Moment.js to format the date. Let we add the locale details to change the data time display.

English Locale:


<script>

    Handlebars.registerHelper("FormatDate", function (date) {              

        moment.locale("en");       

        return moment(date.toString()).format('LLLL');       

    });

</script>

Here is the output

englishLocale

French Locale:


<script>

    Handlebars.registerHelper("FormatDate", function (date) {              

        moment.locale("fr");       

        return moment(date.toString()).format('LLLL');       

    });

</script>

Output

frenchLocale

Good thing is it support my mother tongue “Tamil”. Change the locale to “ta” and the resulted output in tamil.

tamillocale

Following are the list of locales supported by Moment.js

availableLocale

Advertisements

Responses

  1. nice to see tamil working with Sharepoint

  2. good one

  3. […] https://rmanimaran.wordpress.com/2015/04/23/moment-js-change-locale-in-date-time-from-sharepoint-201&#8230; […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: