sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

Problem with dates parsing

Open varunoberoi opened this issue 11 years ago • 6 comments

I have a xlsx file with different date formats, all of which are interpreted as Date type by Excel (2016, Mac). I need to parse these kind of values to native js date objects, but unfortunately js-xlsx interprets them as Number type, see following outputs. I am using js-xlsx v0.8.0 with node v0.12.2. Is it expected behaviour ? How can I parse it to native date objects ?

Outputs:

{ '!ref': 'A1:A12',
  A1: { t: 's', v: 'Dates', r: '<t>Dates</t>', h: 'Dates', w: 'Dates' },
  A2: { t: 'n', v: 41365, w: 'April-13' },
  A3: { t: 'n', v: 41367, w: '03-Apr-13' },
  A4: { t: 'n', v: 41409, w: '5/15/13' },
  A5: { t: 'n', v: 41412, w: '5/18' },
  A6: { t: 'n', v: 41434, w: 'June 9, 2013' },
  A7: { t: 'n', v: 41538, w: '9/21/13 12:00 AM' },
  A8: { t: 'n', v: 41466, w: 'J-13' },
  A9: { t: 'n', v: 41516, w: '30-Aug-13' },
  A10: { t: 'n', v: 41835, w: '15-Jul' },
  A11:
   { t: 's',
     v: '29-02-14',
     r: '<t>29-02-14</t>',
     h: '29-02-14',
     w: '29-02-14' } }

Output of sheet_to_json with raw = true :

[ { Dates: 41365 },
  { Dates: 41367 },
  { Dates: 41409 },
  { Dates: 41412 },
  { Dates: 41434 },
  { Dates: 41538 },
  { Dates: 41466 },
  { Dates: 41516 },
  { Dates: 41835 },
  { Dates: '29-02-14' } ]

varunoberoi avatar Jul 27 '15 07:07 varunoberoi

Hello, In my case I just add my format date (dd/mm/yyyy) in table_fmt array in xlsx.js file

arturhayne avatar Aug 12 '15 19:08 arturhayne

I don't understand . please show me the code....

woaichiji avatar Jan 12 '16 04:01 woaichiji

@varunoberoi see https://github.com/SheetJS/js-xlsx/issues/126 for further information

despairblue avatar Mar 10 '16 10:03 despairblue

var utc_value = Math.floor(your_number- 25569) * 86400; var date_info = new Date(utc_value * 1000); var month = parseInt(date_info.getMonth()) + 1; newDate = date_info.getFullYear() + "/" + month + "/" + date_info.getDate();

pushpenderjunglee avatar Oct 11 '19 12:10 pushpenderjunglee

@pushpenderjunglee that works like a charm, but can you explain a little bit about the magic numbers there?

andrevenancio avatar Oct 30 '19 16:10 andrevenancio

@andrevenancio Excel stores timestamps as a real number representing the number of days since 1 January 1900. 25569 is the number of days between 1 January 1900 and 1 January 1970, which is what we need to convert to a UNIX timestamp that can be used for Date.

arliber avatar Mar 21 '20 07:03 arliber