angulardatedate-pipe

Angular 6 using 'h' as time separator


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.


Solution

  • 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');
    }