GithubHelp home page GithubHelp logo

Comments (24)

pranaysaha avatar pranaysaha commented on July 20, 2024 19

var date = new Date(1899, 0, dataVal - 1);
otherwise in previous cases either it will give +1 day or -1 day.
Eg. 2016-06-01 was my date and after parsing it is showing 42522 days, so in following cases it showed:

  1. var date = new Date(1900, 0, dateVal - 1); 2016-05-31
  2. var date = new Date(1899, 0, dateVal - 1 ); 2015-06-01
  3. var date = new Date(1899, 0, dateVal); 2015-06-02
    so I used following:

var date = new Date(1899, 12, dateVal ); 2016-06-01

from node-xlsx.

theromie avatar theromie commented on July 20, 2024 14

Use this
return new Date ((excelDate - 25567 - 2) * 86400 * 1000);

from node-xlsx.

shyamseshadri avatar shyamseshadri commented on July 20, 2024 10

Do note, just encountered this. Excel has a leap year bug (http://support.microsoft.com/kb/214326) which causes the dates to be off by one if you do @snypelife's suggestion.

So you will need to do an additional -1

var date = new Date(1900, 0, dateVal - 1);

from node-xlsx.

snypelife avatar snypelife commented on July 20, 2024 4

so you may be able to do something like:

var date = new Date(1900, 0, dateFromEpoch)

from node-xlsx.

snypelife avatar snypelife commented on July 20, 2024 1

Did some digging and it appears it's due to the xlsx module that this one depends on:
SheetJS/sheetjs#126

Basically, the number being return is the number of days since the last epoch (1904 or 1900).

from node-xlsx.

Vincentliu89 avatar Vincentliu89 commented on July 20, 2024 1

10:00:00 will be converted to 9:59:59.

from node-xlsx.

SheetJSDev avatar SheetJSDev commented on July 20, 2024 1

Upstream tracking issue SheetJS/sheetjs#1565 . A part of the overall date issues stem from a V8JS (Chrome / NodeJS JavaScript engine) bug which can be tracked at https://bugs.chromium.org/p/v8/issues/detail?id=7863

FWIW none of the simple datecode translations are correct around DST dates like a few days ago (Nov 7 was rollback day for Eastern Time). JS engines perceived the logical day to have 90000 seconds while Excel treats every day as if it has 86400 seconds.

PS @timohausmann in the 1900 date system (most common), the smallest valid datecode is 0. Excel understands this to be "1900-01-00" which is commonly understood by humans to be the midnight of December 31 1899. A number of timezones including Asia/Hong_Kong and Europe/Paris aligned to a v8-friendly timezone after 1900. Depending on your timezone, you will find different dates which "work"

from node-xlsx.

ArvindhMangoleap avatar ArvindhMangoleap commented on July 20, 2024 1

I am having the same issue my excel has 21-Dec-2021 but the returning json shows 44551.
Is there any option that we can pass to sheet_to_json function so that it returns as 21-Dec-2021 without any formating?

from node-xlsx.

snypelife avatar snypelife commented on July 20, 2024

I've noticed this as well. Almost like it's trying to perform an arithmetic operation on the value.

from node-xlsx.

uncedric avatar uncedric commented on July 20, 2024

@shyamseshadri so... this could work?

var date = new Date(1899, 0, dateVal );

from node-xlsx.

snypelife avatar snypelife commented on July 20, 2024

@uncedric little bit late of a reply, so probably don't need an answer anymore, but yes that would work.

from node-xlsx.

snypelife avatar snypelife commented on July 20, 2024

actually, it would have to be var date = new Date(1899, 11, dateValue); i believe.

from node-xlsx.

ericzhao007 avatar ericzhao007 commented on July 20, 2024

@Vincentliu89 I met the same situation,i need help

from node-xlsx.

BumbuKhan avatar BumbuKhan commented on July 20, 2024

Date in my xlsx file was 01.03.2018, to get correct one after parsing I did:

const correctDate = new Date(1899, 12, parsedDate - 1)

then to format it properly I used moment.js lib so finally:

const startDate = moment(new Date(1899, 12, opportunity[2] - 1)).format('YYYY-MM-DD');

from node-xlsx.

pranaysaha avatar pranaysaha commented on July 20, 2024

@Vincentliu89 @zhaoxinwei I need more clarity on your use case. May be u can explain me the date and time format you are storing in excel sheet.
And @BumbuKhan why do you need to do so much? I have tried your date with my solution it is coming perfect date.No problem this. If you are facing some issue please elaborate little bit with your use case.
Thanks

from node-xlsx.

vsnig avatar vsnig commented on July 20, 2024

To fix this "10:00:00 will be converted to 9:59:59"
Do
const date = new Date(1899, 12, dateVal - 0.99999999 );

from node-xlsx.

stale avatar stale commented on July 20, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from node-xlsx.

EvanCarroll avatar EvanCarroll commented on July 20, 2024

I'm still having this issue.

from node-xlsx.

longhaoxuan avatar longhaoxuan commented on July 20, 2024

Use this
return new Date ((excelDate - 25567 - 2) * 86400 * 1000);

utc = new Date();
new Date((excelDate - 25567 - 2) * 86400 * 1000 - utc.getTimezoneOffset*1000*60);

how to optimize

from node-xlsx.

yzh1211 avatar yzh1211 commented on July 20, 2024

hi,i got a float value from Excel like '367.084722222222',how can i convert it to Date in javascript?

from node-xlsx.

zhangchenle666 avatar zhangchenle666 commented on July 20, 2024

The following code supports csv, xls, xlsx:
1.Read file like this

// suffix: file extension
// buffer: use fs.readFile()

if (suffix === 'csv') {
const detact = jschardet.detect(buffer.slice(0, 100))
if (detact.encoding !== 'UTF-8') {
const str = iconv.decode(buffer, detact.encoding)
buffer = iconv.encode(str, 'UTF-8')

// add utf8 BOM Header(3bytes)
const bomBuffer = Buffer.alloc(3)
bomBuffer.fill(239, 0, 1)
bomBuffer.fill(187, 1, 2)
bomBuffer.fill(191, 2, 3)
buffer = Buffer.concat([bomBuffer, buffer], buffer.length + 3)
}
}
const workbook = xlsx.parse(buffer, {raw: true, cellDates: true})

2.and forEach all columns:
if (value instanceof Date)

3.if xlsx:
value = new Date(value.getTime() + 43 * 1000 + 1) // 43seconds an 1 millseconds
then:
const year = value.getFullYear()
let month = value.getMonth() + 1
if (month < 10) month = 0 + month
let date = value.getDate()
if (date < 10) date = '0' + {date}
let hours = value.getHours()
if (hours < 10) hours = '0' + hours
let minutes = value.getMinutes()
if (minutes < 10) minutes = '0' + minutes
let seconds = value.getSeconds()
if (seconds < 10) seconds = '0' + seconds
value = ${year}-${month}-${date} ${hours}:${minutes}:${seconds}

from node-xlsx.

liderdong avatar liderdong commented on July 20, 2024

用这个
return new Date ((excelDate - 25567 - 2) * 86400 * 1000);

May I ask why I have used it for 8 days longer? Is there any solution?

from node-xlsx.

timohausmann avatar timohausmann commented on July 20, 2024

The solutions by theromie, BumbuKhan and shyamseshadri work back to around the year 1600. Sadly, when you have historic data that's older, it gets inaccurate. Time is an issue on it's own. Tests: https://jsfiddle.net/cehu8wgt/

from node-xlsx.

shangzm avatar shangzm commented on July 20, 2024

Use this return new Date ((excelDate - 25567 - 2) * 86400 * 1000);

why?

from node-xlsx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.