我尝试使用Moment.js从html属性解析Javascript日期。
我使用moment(String, String)
函数传递格式字符串,但我得到的内容看起来像是返回的Unix时间戳。我在这里错过了什么?谢谢!
var from = $('#requested-from').attr('data-requested-from');
console.log('from: ' + from);
var requestedFrom = moment(from, 'MM-DD-YYYY');
console.log('requestedFrom: ' + requestedFrom);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="requested-from" class="hidden" data-requested-from=06-10-2017></span>
&#13;
答案 0 :(得分:1)
你需要随时format()
。 MomentJS将时间戳作为默认值返回。
var from = $('#requested-from').attr('data-requested-from');
console.log('from: ' + from);
var requestedFrom = moment(from, 'MM-DD-YYYY');
console.log('requestedFrom: ' + requestedFrom.format('MM-DD-YYYY'));
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="requested-from" class="hidden" data-requested-from=06-10-2017></span>
&#13;