Description:
I am generating an Excel file using the XLSX on an Angular Project (npm install xlsx), but I am facing a problem showing a precise date. If I try to use JavaScript date object it somewhow loses precision and show a wrong date. It also fails if I try to use a string corresponding to 'dd/mm/yyyy'.
I figured out that to make the Microsoft Excel have a precise date with the format dd/mm/yyyy, you need to work with Excel Serial Format, that follows a pattern.
Here is some examples of this format:
- Date: 01/01/1900 - Serial: 1
- Date: 02/01/1900 - Serial: 2
- Date: 09/04/1900 - Serial: 100
- Date: 26/09/1902 - Serial: 1000
- Date: 08/09/1913 - Serial: 5000
- Date: 18/05/1927 - Serial: 10000
- Date: 24/07/1998 - Serial: 36000
*You can try that on Microsoft Excel to check for more examples.
Above here is a prototype of the function I need, that will recevei a date (dd/mm/yyyy) and will return it's serial number equivalent.
private JSDateToExcelDate(inDate) {
if(inDate != null) {
//TO DO
}
}
Additional information:
The xlsx framework has can modify any cell telling it that value type will be Date. With that in mind all I need is a Date in Serial Number format.
UPDATE 11/06/2019 - 20:14
I have found a solution with the following function that provides the number I want. I solved it partialy because the framework XLSX might be changing the value, because I am getting an output as "31/12/1969 21:00:16", witch I found out that is a default output related due to some invalid date. (I have created the function on a Angular Project, so it might contains specific syntax)
private JSDateToExcelDate(date) {
var temp: any;
temp = new Date(date);
if(date != "NaN" && date != "Invalid Date") {
var days = Math.round((temp.setHours(0,0,0,0) - <any>new Date(1899, 11, 30)) / 8.64e7);
let ret:any = (days).toFixed(10);
if(!isNaN(ret)) {
return ret;
}
}
return '';
}