Mercer
Mercer

Reputation: 9986

String value with moment.js

My JS:

...
var dateText='';
dateText = moment(scope.mtxMaxdate,'MM-DD-YYYY');
console.log(dateText);
...

I want to output my value example: '12/12/2014' but in the console i have:

Moment {_isAMomentObject: true, _i: "17/12/2014", _f: "MM-DD-YYYY", _isUTC: false, _pf: Object…}

why..?

Upvotes: 5

Views: 9732

Answers (2)

julian joseph
julian joseph

Reputation: 330

But doing as per the accepted answer, there is a warning of Deprecation that is thrown. Deprecation warning in moment js

However this doesn't seem to throw a warning now. Not sure if the resultant value is how you may need it to be.

> moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]).format('MM-DD-YYYY')
> "12-25-1995"

If you have Date object convert it toString() and then apply the .format()

Upvotes: 0

Florian F.
Florian F.

Reputation: 4700

As stated in momentjs docs you should use .format() function.

Something like this should do it :

var dateText='12-12-2014';
var dateObject = moment(dateText,'MM-DD-YYYY');
console.log(dateObject.format('DD/MM/YYYY'));

The format you give as an argument on second line is just the parse format.

I updated code, the fact that you use angular or not doesn't change a thing. I think what you do not understand is that moment js generates an object from a string date. You can then format this date object just as you want.

Made a jsfiddle in case you don't get it.

Upvotes: 5

Related Questions