iyu/excel2json

Date formate

maheshsetti opened this issue · 14 comments

When i am giving date formate like below:
02/07/2015
02 July 2015
i am getting 4 digits number :P
when i am giving 02-07-2015 it is working fine. so any solution?

iyu commented

This format conforms to Excel date format.
Unix Timestamp = (Excel Timestamp - 25569) * 86400

However parser can be customized by overriding.

var excel2json = require('excel2json');
/**
 * @param {string} d
 * @example
 * _parser.date('02-07-2015');
 * > 1435762800000 // Date.parse('07-02-2015') GMT+0900 (JST)
 */
excel2json._parser.date = function(d) {
  var date = d.split('-');
  return Date.parse(date[1] + '-' + date[0] + '-' + date[2]);
};

It may be necessary to write a string without a ExcelDate If the

if date format "20 jul 2015"
excel2json._parser.date = function(d) {
var date = d.split(' ');
return Date.parse(date[1] + ' ' + date[0] + ' ' + date[2]);
};

above code will work?

iyu commented

It is this and if the case.

/**
 * @param {string} d
 * @example
 * _parser.date('02 jul 2015');
 * > 1435762800000 // Date.parse('02 jul 2015') GMT+0900 (JST)
 */
excel2json._parser.date = function(d) {
  return Date.parse(d);
};
iyu commented
> Date.parse('02 jul 2015') === Date.parse('07-02-2015')
< true
> Date.parse('jul 02 2015') === Date.parse('07-02-2015')
< true

thanks for quick response
i have data like
name, dob, gender in excel when i am doing parse to json
date formate is like 4156(but the orginal date is (02 jul 1992).
if i try like Date.parse(dob);
i am getting some 16 digit number.

iyu commented

I dont know date format like 4156 :|

iyu commented

It is like a ISO8601(YYYY-MM-DD)?

It can be used in javascript it may only YYYY-MM-DD and MM-DD-YYYY.

So we can't use (21 Jul 2015) ?

iyu commented

I answered a little while ago.
It can do if customize the parser.

/**
 * @param {string} d
 * @example
 * _parser.date('21 Jul 2015');
 * > 1437404400000 // Date.parse('21 Jul 2015') GMT+0900 (JST)
 */
excel2json._parser.date = function(d) {
  return Date.parse(d);
};
      var date = value[i]['DATE OF BIRTH'];
              console.log(_parser.date(date));

i am getting output as 1248231212000000(something like this)

how can i get exact date?

any progress in date format?

iyu commented

I canot understand what you want to do.
Could you tell me your actual value and the expected value and what kind of excel data?

when i am giving data format in excel like
08 AUG 2015,
08/08/2015
after parsing excel i will have date in
var date = value[i]['DATE OF BIRTH'];
now i got parsed date in date variable.
if i console date i am getting
42224(some number)
var d = Date.parse(date);
console.log(d);
i am getting
1270293474600000 some 16 digits

final_date = new Date(d);
console.log(final_date);
Thu Jan 01 42224 00:00:00 GMT+0530 (IST)

but i gave 08 Aug 2015

iyu commented

42224 is ExcelDateFormat.
Customize If you want to convert it to UNIXTime is not required.
Please feel free to customize if you want to convert from there further to another.

> var excel2json = require('excel2json')
undefined
> d = excel2json._parser.date(42224)
1438959600000
> new Date(d)
Sat Aug 08 2015 00:00:00 GMT+0900 (JST)