Linked Questions

Popular Questions

AM PM format angular (typescript)

Asked by At

Cannot add formatting for time. Should be displayed in 12 hours, AM PM format; I need this one for a subscription form for customers. The Date and Time I am using for schedule

How to get 12 hours AM PM time format?

private weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
private monthNames = ['January', 'February', 'March', 'April', 'May', 'June',
  'July', 'August', 'September', 'October', 'November', 'December'
];

constructor(private router: Router) {}

ngOnInit() {

}

getCurrentUrl() {
  return this.router.url.split('#')[0];
}

formatDate(dt) {
  const _date = new Date(dt);
  const nameDay = this.weekday[_date.getDay()];
  return nameDay + ', ' + _date.getDate() + ' ' + this.monthNames[_date.getMonth()] + ' ' + _date.getFullYear();
}

getTime(dt) {
    const _date = new Date(dt);
    let _hour = _date.getHours().toString();
    let _min = _date.getMinutes().toString();
    _hour = _hour.length === 1 ? '0' + _hour.toString() : _hour;
    _min = _min.length === 1 ? '0' + _min.toString() : _min;
    return _hour + ':' + _min;

Related Questions