Okay, as you may know, the Windows version of Excel stores dates as sequential serial numbers so they can be used in calculations. Each date is by default assigned a number according to how many days (in GMT) that has passed since January 1, 1900. (That is, January 1, 1900 is serial number 1 and January 1, 2008 is serial number 39448 because it (according to Excel) is 39,448 days after January 1, 1900.)
I need Applescript to store a date variable like that, and I made a code as follows:
set startdate to current date -- just to have a date to alter
set day of startdate to 1
set month of startdate to 1
set year of startdate to 1900
set time of startdate to 0
set datenumber to ((date "jan 1, 2008") - (time to GMT)) - startdate
set datenumber to (datenumber / (days) as integer) + 1
The reason I create a startDate variable that I change to January 1, 1900 instead of simply writing date “jan 1, 1900” is that the code should be language independent. (I’m not sure if “Jan” stands for January in languages.) I don’t need to do the same for the January 2008 date as it won’t be part of the actual code. (If anyone knows of a simpler way to do this (like if you could write “date 01/01, 1900” or something"), just let me know.) The “+ 1” at the end is to make sure January 1, 1900 is considered day 1 rather than day 0. Anyway, the problem is that this code generates 39947 rather than 39948 so one day is lost somewhere. However, when I adjust the code for the current date:
set startdate to current date -- just to have a date to alter
set day of startdate to 1
set month of startdate to 1
set year of startdate to 1900
set time of startdate to 0
set datenumber to ((current date) - (time to GMT)) - startdate
set datenumber to (datenumber / (days) as integer) + 1
I get 38638 which is the same value a PC version (based on the Excel date) of the program I’m writing gives for today.
Can anyone understand this discrepancy? Is the supposed Excel date value for January 1, 2008 (I got it from here) simply incorrect and the actual value IS 39447?
Or does anyone know of a better way to display date in Windows Excel format?
Any help appreciated.