String value using moment. Js

My JS:

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

      

I want to output an example of my value:, '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?..

+3


source to share


2 answers


As stated in the momentjs docs , you have to use the function .format()

.

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 the second line is just the parsing format.

I updated the code, the fact that you are using angular or not doesn't change anything. I think you don't understand that the moment js is generating an object from a string date. You can format this date object however you want.

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

+3


source


But, following the accepted answer, a failure warning is issued. Failure warning at moment js

However, now it doesn't sound like a warning. Not sure if the resulting value is what you might need.



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

      

If you have an object Date

, convert it to toString () and then apply .format ()

0


source







All Articles