1

I am reading excel data using php and JavaScript. Storing results in variable and showing it on the page.

Simple code example:

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

This works for text and fields with number. But when I format cell as "Date" it returns the values, such as.

Excel field is: 31-Dec-2015 - JavaScript returns value: 40542

I know it is a MS DATEVALUE formatting.

But i need to convert it to date using JavaScript so it shows 31-Dec-2015 or 31 December 2015 only.

So in short:

From Excel 40542 to JavaScript 31 December 2015.

Also, I only need as above, without trailing time and locations, so removing:

00:00:00 00:00 GMT

Also is it possible modify the date to +1 day or -1 day?

2

3 Answers 3

2
//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);

 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for help. Above did convert the datevalue to java date, but it is wrong date. var yearend = new Date(("<?php echo ($connection->sheets[0]["cells"][2][5]); ?>" - (25569 - 1)) * 86400*1000); returns: Fri Dec 31 2010 00:00:00 GMT+0000 (GMT) It should be year 2015 not 2010. Any ideas?
Hi again, guys, i have managed to get the date, but now it comes up like Thu Apr 03 2014 01:00:00 GMT+0100 (BST) How do i format it to 3 April 2014?
1

try this

toDate(serialDate, time = false) {
    let locale = navigator.language;
    let offset = new Date(0).getTimezoneOffset();
    let date = new Date(0, 0, serialDate, 0, -offset, 0);
    if (time) {
        return serialDate.toLocaleTimeString(locale)
    }
    return serialDate.toLocaleDateString(locale)
}

Comments

0

Use the following php function to covert the datevalue into a php timestamp. You could then use standard date functions to format however you wish

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.