9

Using DatePipe in Angular 5.1, I need to format the period field type (AM/PM) in lowercase. According to the documentation,

Tuesday, December 19, 7:00 am 

should be

date:'EEEE, MMMM d, h:mm a'

However, the period field type always displays in uppercase, so I see this:

Tuesday, December 19, 7:00 AM

Am I doing something wrong or is this a known defect wtih Angular's date formatting?

Andriy
  • 14,781
  • 4
  • 46
  • 50
ebakunin
  • 3,621
  • 7
  • 30
  • 49
  • 4
    Split it up. - {1234567 l date:'EEEE, MMMM d, h:mm '} {1234567 l date:' a' l lowercase } I have not tried it but should logically work. – Gary Dec 19 '17 at 06:08
  • 1
    please see my updated answer how to achieve it with `date` pipe only – Andriy Dec 21 '17 at 08:58

4 Answers4

24

You can just split your date to 2 parts:

{{ today | date : 'EEEE, MMMM d, h:mm' }} {{ today | date : 'a' | lowercase }}
   

...............

UPDATE

Here is another simple way to achieve it, using built in date pipe and aaaaa matcher (which returns lowercase a or p):

<div>{{ today | date : 'EEEE, MMMM d, h:mm aaaaa\'m\'' }}</div>

Updated Stackblitz: https://stackblitz.com/edit/angular-dcpgzb?file=app%2Fapp.component.html

...............

ANGULAR JS solution

app.controller('MainCtrl', function($scope, $locale) {
  $locale.DATETIME_FORMATS.AMPMS = ['am', 'pm'];
  $scope.today = new Date();
});

https://plnkr.co/edit/a49kjvOdifXPAvBlmXEi?p=preview

pjpscriv
  • 866
  • 11
  • 20
Andriy
  • 14,781
  • 4
  • 46
  • 50
2

Bummer. This is still the case with Angular 5.

I've created a custom pipe which applies a lowercase transform to the text matching a provided regex.

Lowercase Match Pipe

lowercase-match.pipe.ts

import { Pipe, PipeTransform  } from '@angular/core';

@Pipe({
  name: 'lowercaseMatch'
})
export class LowerCaseMatchPipe implements PipeTransform {

  transform (input: any, pattern: any): any {

    if (!this.isString(input)) {
      return input;
    }

    const regexp = pattern instanceof RegExp ? pattern : new RegExp(pattern, 'gi');

    return input.toLowerCase()
    if (input.match(regexp)) {
      return input.toLowerCase()
    }

    return input
  }

  isString(value: any): boolean {
    return typeof value === 'string';
  }
}

Usage

Import to Module

import { LowerCaseMatchPipe } from './lowercase-match.pipe';

@NgModule({
  declarations: [
    ...
    LowerCaseMatchPipe
  ],
  ...
})
export class AppModule { }

Display date with lowercase am/pm

{{ today | date : 'EEEE, MMMM d, h:mm a' | lowercaseMatch : 'am|pm' }}

There is some discussion about this casing notion on an GitHub Issue for Angular https://github.com/angular/angular.js/issues/8763

pjpscriv
  • 866
  • 11
  • 20
Trent
  • 4,208
  • 5
  • 24
  • 46
  • Looks like they won't implement it any time soon, there was a pull request to add lowercase am/pm and it was closed with [this comment](https://github.com/angular/angular.js/pull/8830#issuecomment-139993165) – pjpscriv Aug 04 '21 at 11:38
  • Basically Angular defines it's date/time symbols from the Unicode Common Locale Data Repository (CLDR). And.... *"The CLDR is very extensive, it does not cover every use case. Eg it does not cover lower case version of the time period (ie, am/pm). It would be close to impossible for an independent library to add and maintain a symbol that is not in the CLDR, as the amount of work would be overwhelming and the benefit would be very limited." I would recommend you that you just add the filter lowercase to the expression to work around this limitation."* – pjpscriv Aug 04 '21 at 11:44
1

I want to add onto Andriy's response and combine it into a single interpolation

{{ (today | date: 'MMM d, y, h:mm') + (today | date: 'a' | lowercase) }}

I was having an issue where a space was being added between the two interpolations of Andriy's proposal. That can work if that's what you want, but I needed lowercase with no space: ex. Oct 21, 2020, 1:16pm

derloopkat
  • 6,232
  • 16
  • 38
  • 45
Luke
  • 13
  • 3
1

The form that would best be presented would be:

{{ today | date: 'MMM d, y, h:mm' | titlecase }}

eliseu
  • 15
  • 2