jeti
jeti

Reputation: 1908

Moment.js - getting display format or the formatted value from object

I'm using Angular and Material2 for a project.

I've got a moment object created this way:

myDate = moment.utc(new Date()).format("YYYY-MM-DD HH:mm:ss");

Then this object is passed as a parameter to a different function which needs to know the formatted date string.

Logging this object I see, it has got a property: _f: YYYY-MM-DD HH:mm:ss" and another one: _i: "2018-01-17 13:51:54"

Is there a way I could get the value of _f or _i?

EDIT:
In the end, I want to get "2018-01-17 13:51:54" not really _f or _i.

EDIT2:
myDate acts as a Moment object inside that function. Namely: I'm extending MomentDateAdapter and overriding format(date: Moment, displayFormat: string): string {} So date IS a Moment object. This is used inside Material2 Datepicker.

EDIT3 - some results:
toString: Wed Jan 17 2018 14:10:53 GMT+0100
format: 2018-01-17T14:10:53+01:00
toISOString: 2018-01-17T13:10:53.000Z

Upvotes: 1

Views: 3542

Answers (1)

VincenzoC
VincenzoC

Reputation: 31502

As the Internal Properties guide state you can use:

To print out the value of a Moment, use .format(), .toString() or .toISOString().

instead of _i, while you can use format property returned by creationData() instead of _f.


After your edit (In the end, I want to get "2018-01-17 13:51:54" not really _f or _i), you can simply use format()

let myDate = moment.utc('2018-01-17 13:51:54'); // myDate is a moment object
let cData = myDate.creationData();
console.log( myDate.format("YYYY-MM-DD HH:mm:ss") );
console.log( cData.format );
console.log( myDate.format(cData.format) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>

Upvotes: 4

Related Questions