2015-07-10 115 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; 
}