I want to convert a date like this dd/mm/yyyy to numeric format but I don't find any way to do this in google app script For example I have a date in my "L2" cell so I get the value like this :
date = active_sheet.getRange("L2").getValue()
How to convert date variable to numeric format (integer) ? For example today's date in numeric format is something like 44 000
Thanks in advance
CodePudding user response:
I believe your goal is as follows.
- You want to convert the date object or the date string to the serial number using Google Apps Script.
From I think it's a date object but i'm not sure...
, I thought that 2 patterns can be considered.
Pattern 1:
In this pattern, it supposes that the value of date = active_sheet.getRange("L2").getValue()
is the date object. The sample script is as follows.
var active_sheet = SpreadsheetApp.getActiveSheet();
var date = active_sheet.getRange("L2").getValue();
var serialNumber = (date.getTime() / 1000 / 86400) 25569; // Reference: https://stackoverflow.com/a/6154953
console.log(serialNumber);
Pattern 2:
In this pattern, it supposes that the value of date = active_sheet.getRange("L2").getValue()
is the string value like dd/mm/yyyy
. The sample script is as follows.
var active_sheet = SpreadsheetApp.getActiveSheet();
var date = active_sheet.getRange("L2").getValue(); // or getDisplayValue()
var [d, m, y] = date.split("/");
var d = new Date(y, m, d);
var serialNumber = (d.getTime() / 1000 / 86400) 25569; // Reference: https://stackoverflow.com/a/6154953
console.log(serialNumber);
Testing:
For the above both scripts, when a sample value of 21/04/2022
is used, 44671.625
is returned.