admin 管理员组文章数量: 1086019
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 yourscope.mtxMaxdate
is17/12/2014
but you tell moment that the date you pass has to be in the formatMM-DD-YYYY
, so your date is invalid. – t.niese Commented Dec 16, 2014 at 10:52
2 Answers
Reset to default 5As 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()
本文标签: javascriptString value with momentjsStack Overflow
版权声明:本文标题:javascript - String value with moment.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1743999588a2516198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论