Skip to content

Instantly share code, notes, and snippets.

@omirobarcelo
Created December 9, 2024 19:16
Show Gist options
  • Save omirobarcelo/abe34077bb34a55527b6614a12cb108a to your computer and use it in GitHub Desktop.
Save omirobarcelo/abe34077bb34a55527b6614a12cb108a to your computer and use it in GitHub Desktop.
ngx-countdown Remaining days
import 'zone.js';
import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';
import { CountdownComponent, CountdownConfig } from 'ngx-countdown';
@Component({
selector: 'my-app',
template: `
<countdown [config]="config"></countdown>
`,
standalone: true,
imports: [CountdownComponent],
})
export class AppComponent {
public start = new Date('2025-01-10T20:30:00');
public now = new Date();
public format = (ms: number) => {
const days = Math.floor(ms/(24 * 60 * 60 * 1000));
const daysStr = days > 0 ? `${days}d ` : '';
const hours = Math.floor(ms/(60 * 60 * 1000)) % 24;
const hoursStr = days < 0 && hours < 0 ? '' : `${String(hours).padStart(2, '0')}h `;
const minutes = Math.floor(ms/(60 * 1000)) % 60;
const minStr = days < 0 && hours < 0 && minutes < 0 ? '' : `${String(minutes).padStart(2, '0')}m `;
const seconds = ms/1000 % 60;
const secStr = `${String(seconds).padStart(2, '0')}s`;
return `${daysStr}${hoursStr}${minStr}${secStr}`;
}
// public config: CountdownConfig = {
// leftTime: 86405,
// formatDate: ({ date }) => this.format(date)
// };
public config: CountdownConfig = {
leftTime: (this.start.getTime() - this.now.getTime())/1000,
formatDate: ({ date }) => this.format(date)
};
}
bootstrapApplication(AppComponent).catch((err) => console.error(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment