javascript - String value with moment.js - Stack Overflow

My JS:...var dateText='';dateText = moment(scope.mtxMaxdate,'MM-DD-YYYY');consol

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..?

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..?

Share Improve this question asked Dec 16, 2014 at 10:43 MercerMercer 9,99632 gold badges115 silver badges174 bronze badges 2
  • Why do you expect that dateText will be a string. moment: String + Format creates a moment date object out of a string using the given format. – t.niese Commented Dec 16, 2014 at 10:46
  • As _i shows your scope.mtxMaxdate is 17/12/2014 but you tell moment that the date you pass has to be in the format MM-DD-YYYY, so your date is invalid. – t.niese Commented Dec 16, 2014 at 10:52
Add a ment  | 

2 Answers 2

Reset to default 5

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.

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()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743999587a4541259.html

相关推荐

  • javascript - String value with moment.js - Stack Overflow

    My JS:...var dateText='';dateText = moment(scope.mtxMaxdate,'MM-DD-YYYY');consol

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信