Priyanka
Priyanka

Reputation: 390

Angular: how to convert time from 00:00:01 to 8 min 49 second format?

I want to convert time from 00:00:01 (which is not a date object) to 8 min 49 second format.

I went through Angular 'date pipe' documentation also but I didn't get any solution.

Upvotes: 0

Views: 1761

Answers (1)

Akber Iqbal
Akber Iqbal

Reputation: 15031

Angular date pipes is the way to go... This should work for you in your HTML:

Update: from your comments, i gather that you have a time string, due to which you'd first have to convert that time string into a date... after that, the Angular Date pipe will handle the rest;

relevant TS:

  priyankaTime = '08:04:01';
  priyankaDate;
  constructor() {

    this.priyankaFunction()
  }

  priyankaFunction() {
    var origStr = this.priyankaTime;
    var n = origStr.search(":");
    var hrPart = origStr.substring(0, n);

    var str = origStr.substring(n + 1, origStr.length);
    var n = str.search(":");
    var minPart = str.substring(0, 2);

    var str = str.substring(n + 1, str.length);
    var n = str.search(":");
    var secPart = str.substring(0, 2);
    console.log(hrPart, ':', minPart, ':', secPart);

    this.priyankaDate = new Date();
    this.priyankaDate.setHours(hrPart, minPart, secPart);

  }

relevant HTML:

{{myDateVariable | date:"h 'hours' mm 'minutes' ss 'seconds'" }}

check updated working stackblitz here

Upvotes: 1

Related Questions