1

I am having a variable which is a NgbDateStruct object in the typescript file and would like to convert it into a String of the following format: "YYYY-MM-DD".

 currentDate: NgbDateStruct;

When I console.log(this.currentDate) it is showing a Moment object.

console.log(this.currentDate);

Moment {_isAMomentObject: true, _i: '2021-12-1', _f: 'YYYY-MM-DD', _isUTC: false, _pf: {…}, …} _d: Wed Dec 01 2021 00:00:00 GMT+0800 (Singapore Standard Time) {} _f: "YYYY-MM-DD" _i: "2021-12-1" _isAMomentObject: true _isUTC: false _isValid: true _locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: 'Invalid date', _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, ordinal: ƒ, …} _pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(0), overflow: -1, charsLeftOver: 0, …} [[Prototype]]: Object

How would I be able to extract the format "YYYY-MM-DD" that is already existing in this object that is shown in the console.log?

Nic
  • 127
  • 1
  • 11

1 Answers1

0

If it is a moment object then you can get string date by formatting it this way

console.log(this.currentDate.format("YYYY-MM-DD"));
zainhassan
  • 1,684
  • 6
  • 12
  • Yes I have tried that earlier but NgbDateStruct does not have a property format unlike Date. – Nic Dec 03 '21 at 07:46