1

Is there any built-in easy way to use 'h' as hours/minutes separator? I am trying to do something like this "12h45" as per:

For other countries it is quite easy e.g.

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

@Pipe({
  name: 'dateFormat'
})
export class DateFormatPipe extends DatePipe implements PipeTransform {

  transform(value: any, args?: any): any {

    switch (args) {
      case 'en': {
        return super.transform(value, 'MM/dd/yyyy h:mm a');
      }

      case 'fr': {
        return super.transform(value, 'dd/MM/yyyy HH:mm'); // <--- find proper separator
      }

      case 'de': {
        return super.transform(value, 'dd.MM.yyyy HH:mm');
      }

      case 'es': {
        return super.transform(value, 'yyyy/MM/dd hh:MM');
      }
    }
  }
}

NOTE: selected date formats might not be true or in everyday use, this is just for demo purpose.

Nenad Bulatović
  • 7,238
  • 14
  • 83
  • 113

2 Answers2

2

No, there's no built-in solution to use 'h' as separator. Although, as you can see here, the transform() methods always return either string or null. You can leverage on this and use the .replace() method to substitute the semicolon with the h letter:

case 'fr': {
    const transformedDate = super.transform(value, 'dd/MM/yyyy HH:mm');
    return transformedDate?.replace(':', 'h');
}
2

You can also do either of these two:

super.transform(value, 'H\'h\'mm');
super.transform(value, "H\'h\'mm");

Anything in the format string that is enclosed in single quotes is shown as is. So if value has time as 1:15pm, this will return 13h15.

See it work on this fiddle: https://stackblitz.com/edit/angular-wvtdpw

Frank Fajardo
  • 7,034
  • 1
  • 29
  • 47