2014-11-25 170 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