2014-11-25 159 views
-3

在这里,我想格式化billDetail.invoiceDatedd-mm无法格式化日期

我的代码是

$.each(data.billDetails, function(position, billDetail) { 

     if (billDetail.invoiceDate) { 
      //I tried 
      var dat = (billDetail.invoiceDate).format("dd-mm");//Showing error 
      chartData.labels.push(dat); 
     } else { 

     chartData.labels.push(''); 
     } 

     chartData.datasets[0].data.push(billDetail.totalBills); 
     chartData.datasets[1].data.push(billDetail.totalAmount); 
    }); 

显示错误

Uncaught TypeError: undefined is not a functiondashboard.action:221 
(anonymous function)jquery-2.1.1.min.js:2 n.extend.eachdashboard.action:218 
$.ajax.successjquery-2.1.1.min.js:2 jjquery-2.1.1.min.js:2 k.fireWithjquery-2.1.1.min.js:4 
    xjquery-2.1.1.min.js:4 (anonymous function) 

我的数据在控制台给出的是

dashboard.action:191 2014-11-25T00:00:00 4 1545 
dashboard.action:191 2014-11-24T00:00:00 6 24497 
dashboard.action:191 2014-11-23T00:00:00 1 114 
dashboard.action:191 2014-11-22T00:00:00 1 114 
dashboard.action:191 2014-11-18T00:00:00 5 4916 
dashboard.action:191 2014-11-13T00:00:00 7 29040 
dashboard.action:191 2014-11-01T00:00:00 4 7317 
dashboard.action:191 2014-10-31T00:00:00 7 53061 
dashboard.action:191 2014-10-30T00:00:00 1 114 
+0

是'billDetail.invoiceDate'日期对象或字符串..也没有日期对象的'.format()'函数...您需要使用一些第三方库来做到这一点 – 2014-11-25 08:31:23

+0

@ArunPJohny plese se updated问题 – xrcwrn 2014-11-25 08:57:18

回答

1

只需使用getDate()getMonth()Date Object Methods

您的billDetail.invoiceDate是一个字符串,所以使它成为日期然后管理它。

var d = new Date(billDetail.invoiceDate); 
var dat = d.getDate()+"-"+parseInt(d.getMonth()+1); 

这是the working fiddle

这应该做到这一点。

+2

请记住,JavaScript月份为0索引,而日期为1索引。 – chridam 2014-11-25 08:43:03

+0

显示'未捕获的类型错误:undefined不是函数' – xrcwrn 2014-11-25 08:44:08

+0

@chridam是的,其实你是对的我编辑了代码。 – 2014-11-25 08:44:34