home
  • Blog
1.10
  • Getting Started
  • Getting Ember
  • Concepts
  • The Object Model
  • Application
  • Templates
  • Routing
  • Components
  • Controllers
  • Models
  • Views
  • Enumerables
  • Testing
  • Configuring Ember.js
  • Cookbook
    • Introduction
    • Contributing
    • User Interface and Interaction
      • Introduction
      • Adding CSS Classes to Your Components
      • Adding CSS Classes to Your Components Based on Properties
      • Focusing a Textfield after It's Been Inserted
      • Displaying Formatted Dates With Moment.js
      • Specifying Data-Driven Areas of Templates That Do Not Need To Update
      • Using Modal Dialogs
      • Resetting scroll on route changes
    • Event Handling & Data Binding
    • Helpers & Components
    • Working with Objects
  • Understanding Ember.js
  • Contributing to Ember.js
Old Guides - You are viewing the guides for Ember v1.10.0.
Go to v5.0.0

Displaying Formatted Dates With Moment.js

Edit pencil

Problem

Display JavaScript Date objects in human readable format.

Solution

There are two ways of formatting the value:

  1. Create a Handlebars helper {{format-date}} and use it in your template
  2. Create a computed property formattedDate that will return a transformed date

We will use Moment.js for formatting dates.

Let's look at a simple example. You're working on a website for your client, and one of the requirements is to have the current date on the index page in human readable format. This is a perfect place to use a Handlebars helper that "pretty prints" the current date:

Ember.Handlebars.registerBoundHelper('currentDate', function() {
  return moment().format('LL');
});

Your template will look like:

Today's date: {{currentDate}}  // Today's date: August 30 2013

You can even enhance your code and pass in the date format to the helper:

Ember.Handlebars.registerBoundHelper('currentDate', function(format) {
  return moment().format(format);
});

Now you would need to pass an additional parameter to the helper:

Today's date: {{currentDate 'LL'}}  // Today's date: August 30 2013

Let's look at another example. Say you need to create a simple control that allows you to type in a date and a date format. The date will be formatted accordingly.

Define formattedDate computed property that depends on date and format. Computed property in this example does the same thing as Handlebars helpers defined above.

App.ApplicationController = Ember.Controller.extend({
  format: "YYYYMMDD",
  date: null,
  formattedDate: function() {
    var date = this.get('date'),
        format = this.get('format');
    return moment(date).format(format);
  }.property('date', 'format')
});
{{input value=date}}
{{input value=format}}
<div>{{formattedDate}}</div>

Discussion

Both helper and computed property can format your date value. Which one do I use and when?

Handlebars helpers are shorthand for cases where you want to format a value specifically for presentation. That value may be used across different models and controllers.

You can use {{currentDate}} across your application to format dates without making any changes to controllers.

Computed property in the example above does the same thing as the Handlebars helper with one big difference: formattedDate can be consumed later without applying date format on the date property again.

Example

JS Bin

left arrow
Focusing a Textfield after It's Been Inserted
Specifying Data-Driven Areas of Templates That Do Not Need To Update
right arrow
On this page

  • Problem
  • Solution
  • Discussion
  • Example
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2023 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue