Ben Racicot
Ben Racicot

Reputation: 5895

Using Angular DateTime pipe programmatically for hh:mm:ss formats

I'm trying use DatePipe for HH:MM:SS (or any other documented formats) just as I've used DecimalPipe for monetary values.

However, passing my logic a format such as hh:mm:ss my values immediately turn to 07:00:00.

time = 'hh:mm:ss';
val = '1:02'; // user entered value
invalids = new RegExp('[^0-9:]{0,6}', 'g'); // time chars only
replaced = String(val).replace(invalids, ''); // remove non-hhmmss characters
let value = this.datePipe.transform(replaced, time); // format the value
console.log(value); // '07:00:00' why!?

Here is the repro in StackBlitz:67

How can I get the component to accept those predefined formats to force the input value?

Upvotes: 0

Views: 2149

Answers (1)

cjd82187
cjd82187

Reputation: 3593

Angular DatePipe's input is typed as any, but actually requires a Date, or miliseconds, or ISO formatted string which you are not giving it.

https://angular.io/api/common/DatePipe#input-value

value any The date expression: a Date object, a number (milliseconds since UTC epoch), or an ISO string (https://www.w3.org/TR/NOTE-datetime).

The data you are passing in does not meet that criteria so it's probably not going to work for you use case unless you can modify it before sending to datePipe.

Upvotes: 1

Related Questions