I know that you are happy with your decision in its current form, but I just wanted to add my comments on how Google Apps Script deals with "dates", either passed in a user-defined function or retrieved from a cell using getValue ().
My rule of thumb is that if the worksheets (spreadsheet application) provide a value formatted as a date (either by automatic coercion or by a user specifying a format), then Google Apps Script automatically holds that value as a date object.
For example:
function returnDate(value) { return new Date(value); }
If you enter 1/1/13 into A1, and in another cell you call =returnDate(A1) , it will return the same date (as if you just had return value; in the code). However, see what happens when you format A1 as "Normal" (convert it to a numeric value). Here, the "Serial Number of the Sheet" (number of days from 30/12/1899) is converted to a date object using Google Apps Script, but in GAS it is "considered" as the number of milliseconds since midnight 1/1/1970. This way, you can get unexpected results if you pass in numerical values ββthat you think are representative of the date.
Also compare:
=returnDate(DATE(2013;1;1))
=returnDate(VALUE("1/1/13"))
=returnDate(DATEVALUE("1/1/13"))
=returnDate("1/1/13")
=returnDate("1/1/2013")
The last two βworkβ because new Date() successfully creates a date object from a valid string, but note that Sheets is automatically snapped to the current century, and GAS provides a two-digit year to the 1900s.
So IMO, if you want it to behave the same way as in Excel (that is, "treat" the numeric value as the serial number for the date), you will need to first check if the passed parameter is a date object (or "valid "text string), and if not, mathematically convert it from" days from 30/12/1899 "to" milliseconds from 1/1/1970 ", and then new Date() .
Apologies for the long post.
Adaml
source share