cipchk / ngx-countdown

Simple, easy and performance countdown for angular
https://cipchk.github.io/ngx-countdown/
MIT License
192 stars 57 forks source link

stopTime with date format #92

Closed Nabeeh-AlSawaf closed 3 years ago

Nabeeh-AlSawaf commented 3 years ago

Bug Report or Feature Request (mark with an x)


[x] Bug report -> please search issues before submitting
[ ] Feature request
[x] Documentation issue or request

Current behavior

when given unix timestamp (1614600000000) and format like 'yyyy-MM-dd E HH:mm:ss a' it gives 1970-01 ..etc example : <countdown [config]="{ stopTime: 1614600000000 ,format: 'yyyy-MM-dd E HH:mm:ss a' }">

Expected behavior

to be correct date of the year month and day 2021 - 03 - 01 14:00:00 image

Environment


Angular version: X.Y.Z

ngx-countdown version: X.Y.Z

Browser:
- [ ] Chrome (desktop) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] IE version XX

Others:

Nabeeh-AlSawaf commented 3 years ago

the following fixed the problem for me in html <countdown [config]="{ stopTime: 1614600000000 ,format: 'DD:HH:mm:ss' ,formatDate : formatDate}"></countdown> in script CountdownTimeUnits: Array<[string, number]> = [ ['Y', 1000 60 60 24 365], // years ['M', 1000 60 60 24 30], // months ['D', 1000 60 60 24], // days ['H', 1000 60 60], // hours ['m', 1000 60], // minutes ['s', 1000], // seconds ['S', 1] // million seconds ]; formatDate?: CountdownFormatFn = ({ date, formatStr, timezone }) => { let duration = Number(date || 0);

return this.CountdownTimeUnits.reduce((current, [name, unit]) => { if (current.indexOf(name) !== -1) { const v = Math.floor(duration / unit); duration -= v * unit; return current.replace(new RegExp(${name}+, 'g'), (match: string) => { return v.toString().padStart(match.length, '0'); }); } return current; }, formatStr); };