Skip to content

{{format-date}}

Uses Intl.DateTimeFormat to format a date.

gts
import type { TOC } from '@ember/component/template-only';
import { formatDate } from 'ember-intl';

interface ExampleSignature {
  Args: {};
}

const today = new Date();

const Example: TOC<ExampleSignature> = <template>
  {{formatDate today}}
</template>;

export default Example;

options.format

In app/ember-intl.{js,ts}, you can use the formatDate key to define the formats that you want to reuse for the helper. Pass the name of your format to format.

gts
import type { TOC } from '@ember/component/template-only';
import { formatDate } from 'ember-intl';

interface ExampleSignature {
  Args: {};
}

const today = new Date();

const Example: TOC<ExampleSignature> = <template>
  {{formatDate today format="user-friendly"}}
</template>;

export default Example;
ts
import type { Formats } from 'ember-intl';

export const formats: Formats = {
  formatDate: {
    'user-friendly': {
      day: 'numeric',
      month: 'long',
      weekday: 'short',
    },
  },
};

options.locale

You can display the text in another locale (i.e. independently from the user's preferred locale). Pass the name of the locale to locale.

gts
import type { TOC } from '@ember/component/template-only';
import { formatDate } from 'ember-intl';

interface ExampleSignature {
  Args: {};
}

const today = new Date();

const Example: TOC<ExampleSignature> = <template>
  <div lang="en-us">
    {{formatDate today locale="en-us"}}
  </div>

  <div lang="de-de">
    {{formatDate today locale="de-de"}}
  </div>
</template>;

export default Example;

Additional options

You can use named arguments to pass the options that Intl.DateTimeFormat supports. Some of these options are listed below.

  • hour12
  • dateStyle
  • timeZone
  • weekday
gts
import type { TOC } from '@ember/component/template-only';
import { formatDate } from 'ember-intl';

interface ExampleSignature {
  Args: {};
}

const today = new Date();

const Example: TOC<ExampleSignature> = <template>
  {{formatDate today dateStyle="full" timeZone="America/New_York"}}
</template>;

export default Example;