Home > Net >  How to use datepipe outside of a component.ts in angular?
How to use datepipe outside of a component.ts in angular?

Time:12-01

I have a typescript file used to export some constant variables (export const). I would like to use datepipe to format date in the ts file, but I do not know how to add service to use datepipe in a non component ts file.

For example:

export const dateObject = [
  {
    value: {
      start: new Date(),
      end: new Date().setFullYear(new Date().getFullYear()   10),
    },
    title: 'Ten Years Later'
  }
];

How can I make datepipe.transform(, 'yyyy-MM-dd') work in this typescript?

Thank you!!

CodePudding user response:

import { DatePipe } from '@angular/common';

export const dateObject = [{
  value: {
    start: new DatePipe().transform(new Date(), 'yyyy-MM-dd'),
    end: new Date().setFullYear(new Date().getFullYear()   10),
  },
  title: 'Ten Years Later',
}];

CodePudding user response:

new DatePipe(navigator.language || 'en-US').transform(new Date(), 'yyyy-MM-dd')
  • Related