如何将“Mon Mar 14 2014 00:00:00 GMT + 0530(IST)”转换为2014-03-08使用Moment.js
问题描述:
在AngularJS中,即在controller.js文件中,我正在从此数据库检索日期格式即“2014年3月14日00:00:00 GMT + 0530(IST)”。但是当我在html页面上使用AngularJS datepicker显示这个日期时,它显示为空意味着它没有显示这种格式的日期,即2014-03-08。如何将“Mon Mar 14 2014 00:00:00 GMT + 0530(IST)”转换为2014-03-08使用Moment.js
答
这里的例子就如何使用时刻JS
<div id="date"></div>
<div id="momentdate"></div>
<script>
$(document).ready(function() {
var d = new Date(); //Sat Mar 05 2016 17:48:48 GMT+0530 (India Standard Time)
$('#date').text(d);
var md = moment(d).format('YYYY-MM-DD');
$('#momentdate').text(md);
});
</script>
输出
Sat Mar 05 2016 17:48:48 GMT+0530 (India Standard Time)
2016-03-05
工作示例转换日期格式上jsfiddle
不过的.text()不angularjs控制器 – satheeshreddy
来支持显示我使用过的结果。您需要的日期格式为:(d).format('YYYY-MM-DD');' –
您可以添加一段代码吗? –