Date format issue dd/mm/yyyy
We are using sheetjs for exporting report which having date coulmns in dd/mm/yyyy format. The exported file converts the date to to mm/dd/yyyy format for those date are less than 13 and other dates are exported as desired. We need to export file with dd/mm/yyyy format itself
How to fix this issue? Or what is the way to disable autoformatting of date column and consider it as string itself?
Sample Code as follows:
<table id="table">
<tr>
<td>1</td>
<td>05/04/2021</td>
<td>Fifth April, Monday</td>
</tr>
<tr>
<td>2</td>
<td>06/04/2021</td>
<td>Sixth April, Monday</td>
</tr>
<tr>
<td>3</td>
<td>11/04/2021</td>
<td>Eleventh April, Sunday</td>
</tr>
<tr>
<td>4</td>
<td>12/04/2021</td>
<td>Twelvth April, Monday</td>
</tr>
<tr>
<td>5</td>
<td>13/04/2021</td>
<td>Thirtheenth April, Monday</td>
</tr>
<tr>
<td>6</td>
<td>14/04/2021</td>
<td>Fourtheenth April, Monday</td>
</tr>
</table>
var wb = XLSX.utils.table_to_book(document.getElementById('table'));
XLSX.writeFile(wb, "sample.xlsx");
Sample JSFiddle is here
We have found some solution like below
var wb = XLSX.utils.table_to_book(document.getElementById('table'), {dateNF:'dd/mm/yyyy;@',cellDates:true, raw: true});
Fiddle is here
Suggest us if there are any better solution to apply dateNF to only a selected column in the sheet? the above option is converting all the columns to string.
If ever someone will use try to use the option date with bulk write
XLSX.utils.sheet_add_json
which not have the dateNF function
the best solution to have correct date format is to use the SSF algorithm
ex:
date - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
P.S
My use of the above dateNF function using the (provided fiddle link ) will not produce an Excel with date format when looking on the cells. (Excel 2019)
But we assume Date.UTC(1899, 11, 30) is having dependancy and it is different for different OS systems.
There are two epochs (January 1 1900 and January 1 1904) - see in the docs
So you can normalize it like that by adding 1462 to the value.
i have some issue, is there any way someone can help? i am uploading an excel and xlsx.read is converting date in my excel In Date(); format of JS but I dont want that i want this has to be treated as string. no conversion in Date object. any suggestions?
There are a few issues here:
@mpsbhat is asking about parsing of ambiguous dates. It is unclear whether "2/1/21" represents "January 02" or "February 01". There are two workarounds:
A) If you want to bypass value parsing entirely, pass raw: true to table_to_book:
var wb = XLSX.utils.table_to_book(document.getElementById('table'));
B) If you are generating the HTML table, you can add t, v, z attributes to the TD element. If the DOM walker sees those attributes, it will use them in lieu of parsing the text. For dates you would set t="d" and set v to the ISO8601 string of the date you want.
@nivb52 If you have full control over what value you pass, you can actually pass cell objects like {t:"n", v:45544, z:"m/d/yy"} instead of the raw date codes. The writers will use the specified number format and Excel will treat them like dates.
@Triyank passing raw: true will disable date parsing
my workaround:
function exportTableToExcel(tableID, filename = '') {
var elt = $(tableID + ' .pvtTable').clone()[0];
for (let i = 0; i < elt.rows.length; i++) {
for (let j = 0; j < elt.rows[i].cells.length; j++) {
let cell = moment(elt.rows[i].cells[j].innerHTML, 'DD/MM/YYYY');
if (cell.isValid()) {
elt.rows[i].cells[j].innerHTML = cell.toISOString();
}
}
}
var wb = XLSX.utils.table_to_book(elt, {sheet:"Sheet"});
return XLSX.writeFile(wb, filename + '.xlsx');
}