Anybody know of a way to take a given date and figure out how many days away from today that date is?
For example… I can get todays date various ways…
In AppleScript:
current date
returns this: date “Thursday, February 17, 2005 3:45:36 PM”
Or you can do this:
do shell script “date”
returns this: “Thu Feb 17 15:48:53 CST 2005”
do shell script “date ‘+%y%m%d%H%M%S’”
returns this: “050217154705”
I’m able to get the logged in user’s password expiration via LDAP in the following way:
do shell script “ldapsearch -x -h 10.1.254.29 -LLL cn=whoami passwordExpirationTime”
returns this: "dn: cn=username,ou=TECH,o=SG
passwordExpirationTime: 20050222162134Z
"
Which means their password expires on Feb. 22, 2005 at 4:21:34 pm.
I can parse out the date/time info fine, but how do I compare it to the current date/time to fill in the XX blank “Your password will expire in XX days.”
Do I need to define how many days are in each month in order to span months? Any ideas?
set date1 to "1/1/2001"
set date2 to "2/2/2001"
set date1 to date date1
set date2 to date date2
set diff to date2 - date1 --> 2764800 (seconds)
set daysDiff to diff / 86400 --> 86400 = seconds in a day
--> 32
Find the difference in seconds. Then use the ‘div’ operator and constant ‘days’. Something like this:
set first_day to date “Saturday, January 1, 2005 12:00:00 AM”
set cur_date to (current date)
set seconds_diff to (cur_date - first_day) – difference in seconds
set days_diff to seconds_diff div days – number of days since the first day
Further to jj’s and kel’s replies, a nice, fast way to get from parsing the shell script return to the result you want would be:
set shellScriptReturn to "dn:cn=username,ou=TECH,o=SG
passwordExpirationTime:20050222162134Z
" as Unicode text -- for demo purposes
set n to (text 1 thru 14 of word -1 of shellScriptReturn) as number
set expiryDate to date "Wednesday 1 January 1000 00:00:00" -- any January date
set expiryDate's year to n div 1.0E+10
set expiryDate's day to (n mod 1.0E+10 div 100000000 - 1) * 32 -- overflows to expiry month
set expiryDate's day to n mod 100000000 div 1000000
set expiryDate's time to n mod 1000000 div 10000 * hours + n mod 10000 div 100 * minutes + n mod 100
set msg to "Your password will expire in " & ((expiryDate - (current date)) div days) & " days."
Alas, it isn’t. It’ll error with January dates when it tries to set the day to 0. Sorry. :oops:
Here’s a more reliable version:
set shellScriptReturn to "dn:cn=username,ou=TECH,o=SG
passwordExpirationTime:20050222162134Z
" --as Unicode text -- for demo purposes
set n to (text 1 thru 14 of word -1 of shellScriptReturn) as number
set expiryDate to date "Monday, 1 December 1000 00:00:00" -- any December date
set expiryDate's year to n div 1.0E+10 - 1 -- the year before the expiry date
set expiryDate's day to (n mod 1.0E+10 div 100000000) * 32 -- overflow to the expiry month
set expiryDate's day to n mod 100000000 div 1000000
set expiryDate's time to n mod 1000000 div 10000 * hours + n mod 10000 div 100 * minutes + n mod 100
set msg to "Your password will expire in " & ((expiryDate - (current date)) div days) & " days."