2015-07-10 149 views
1

我正在閱讀使用php和JavaScript的excel數據。將結果存儲在變量中並將其顯示在頁面上。將excel DATEVALUE轉換爲javascript日期

簡單的代碼示例:

var yearend = "< ? php echo ($connection->sheets[0]["cells"][2][5]); ? >"; 

這適用於文本和字段與數量。但是,當我將單元格格式化爲「日期」時,它會返回值,例如。

Excel的領域是:31-Dec-2015 - JavaScript的返回值:40542

我知道這是一個MS DATEVALUE格式。

但我需要使用JavaScript將其轉換爲日期,因此它只顯示31-Dec-201531 December 2015

因此,在短期:

從Excel中40542爲JavaScript 31 December 2015

另外,我只需要如上,無拖尾時間和地點,以便消除:

00:00:00 00:00 GMT 

也有可能修改日期+1天或1天?

+0

datetime :: createfromformat和datetime :: format,基本上。將Excel日期字符串加載到DateTime對象中,然後根據需要將其格式化。 –

+0

[將MS Excel日期/時間轉換爲Unix時間戳值的快捷公式](http://stackoverflow.com/questions/28167196/spreadsheet-excel-reader-date-format/28167908#28167908) –

回答

0
//Convert Excel dates into JS date objects 

//@param excelDate {Number} 
//@return {Date} 

function getJsDateFromExcel(excelDate) { 

// JavaScript dates can be constructed by passing milliseconds 
// since the Unix epoch (January 1, 1970) example: new Date(12312512312); 

// 1. Subtract number of days between Jan 1, 1900 and Jan 1, 1970, plus 1 (Google "excel leap year bug")    
// 2. Convert to milliseconds. 

return new Date((excelDate - (25567 + 1))*86400*1000); 

} 
+0

謝謝求助。上面沒有將datevalue轉換爲java日期,但它是錯誤的日期。 ((「<?php echo($ connection-> sheets [0] [」cells「] [2] [5]);?>」 - (25569 - 1))* 86400 * 1000 ); 回報:Fri Dec 31 2010 00:00:00 GMT + 0000(GMT) 它應該是2015年,而不是2010年。任何想法? – Dimitry

+0

嗨,夥計, 我設法得到日期,但現在它出現像星期四2014年4月1日01:00:00 GMT + 0100(BST)我如何格式化到2014年4月3日? – Dimitry

0

使用以下php函數將datevalue轉換爲php時間戳。然後,您可以使用標準日期函數格式,但是您希望

function xl2timestamp($xl_date){ 
    return ($xl_date - 25569) * 86400; 
}