[英]How to format this JSON data to date(mm/dd/yyyy)
Sample JSON: 样本JSON:
[{"DispatchDt":"2014-05-28T01:34:00","RcvdDt":"1988-12-26T00:00:00"}]
I have this set of dates and I want to convert it to the date format (mm/dd/yyyy). 我有这组日期,我想将其转换为日期格式(mm / dd / yyyy)。 How do you do this is JavaScript?
您如何使用JavaScript?
Unfortunately parsing and formatting dates is a weak side of javascript. 不幸的是,解析和格式化日期是javascript的弱项。
So usually for that we use the 3rd party libraries like moment.js 因此通常为此,我们使用第三方数据库,例如moment.js
An example of how you would do that with moment.js
: 有关如何使用
moment.js
:
var date = '2014-05-28T01:34:00';
var parsedDate = moment(date);
var formattedDate = parsedDate.format('MM/DD/YYYY');
Demo: http://jsfiddle.net/8gzFW/ 演示: http : //jsfiddle.net/8gzFW/
You may format the json string into new Date()
then use js methods to get month,day,year or what exactly you need. 您可以将json字符串格式化为
new Date()
然后使用js方法获取月,日,年或您真正需要的东西。
//format string to datetime
var DispatchDt = new Date(this.DispatchDt);
// then use this methods to fetch date,month, year needed
//getDate() // Returns the date
//getMonth() // Returns the month
//getFullYear() // Returns the year
var DispatchDt_date = DispatchDt.getDate();
var DispatchDt_month = DispatchDt.getMonth() + 1; //Months are zero based
var DispatchDt_year = DispatchDt.getFullYear();
This would work 这会工作
var a = "[{"DispatchDt":"2014-05-28T01:34:00","RcvdDt":"1988-12-26T00:00:00"}]";
var b = JSON.parse(c);
for (i in b[0]) {
b[0][i] = b[0][i].slice(0, 9)
}
console.log(b); // b now looks like this [ { DispatchDt: "2014-05-28", RcvDt: "1988-12-26" } ]
Please note the dates are stringified. 请注意,日期是字符串化的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.