简体   繁体   English

将 NgDateStruct object 转换为字符串

[英]Converting a NgDateStruct object into a String

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".我有一个变量,它是 typescript 文件中的 NgbDateStruct object 并希望将其转换为以下格式的字符串:“YYYY-MM-DD”。

 currentDate: NgbDateStruct;

When I console.log(this.currentDate) it is showing a Moment object.当我 console.log(this.currentDate) 显示一个 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时刻{_isAMomentObject:真,_i:'2021-12-1',_f:'YYYY-MM-DD',_isUTC:假,_pf:{...},...} _d:格林威治标准时间 2021 年 12 月 1 日星期三 00:00:00 +0800(新加坡标准时间){} _f: "YYYY-MM-DD" _i: "2021-12-1" _isAMomentObject: true _isUTC: false _isValid: true _locale: Locale {_calendar: {…}, _longDateFormat: {… }, _invalidDate: '无效日期', _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/, 序数: ƒ, …} _pf: {empty: false, usedTokens: Array(0),未使用的输入:数组(0),溢出:-1,charsLeftOver:0,...} [[原型]]: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?我如何能够提取 console.log 中显示的 object 中已经存在的格式“YYYY-MM-DD”?

If it is a moment object then you can get string date by formatting it this way如果是片刻 object 那么你可以通过这种方式格式化它来获取字符串日期

console.log(this.currentDate.format("YYYY-MM-DD"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM