convert a string into a date

Hi,
is it possible to convert a string into a date? I have a text file that contains a date e.g. “30/10/07”, I would then like to convert that string into a date format so that I may add 7 days to the date string! I know that I can add 7 days to current date like so

set new_date to (current date) + 7 * days

display dialog (short date string of (new_date))

but I need to be able to specify a date rather than using (current date). The below code doesn’t work but it gives a better idea of what I mean.

set mydate to "30/10/07"

set new_date to (mydate) + 7 * days

Thanks,
Nik

After a little trial and error I think I’ve worked it out!

set user_date to text returned of (display dialog "enter start date" default answer "24/09/07")
set mydate to date user_date
set new_date to (mydate) + 7 * days
set new_short_date to (short date string of (new_date))

Thanks for lokking,
Nik

I doubt if your method will work in all cases. Try this:

set D to text returned of (display dialog "enter start date" default answer "29/09/07")
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
tell text items of D to set isot to ("20" & item 3 & item 2 & item 1 & "T") as Unicode text
set AppleScript's text item delimiters to tid
set {text:U} to isot as Unicode text as string -- convert to ASCII text
set newDate to short date string of ((U as «class isot» as date) + 7 * days)

Hi Adam,
thanks for your response. What problems do you foresee I’ll have with this

set user_date to text returned of (display dialog "enter start date" default answer "24/09/07")
set new_date to (date user_date) + 7 * days
set new_short_date to (short date string of (new_date))

instead of your code?
Thanks,
Nik

The action of coercing a string into a date and the use of date/time string is dependent on the current system’s International settings (the language and date formats, respectively).

For example, your code gets these values on my system:
date “Saturday, 14 September 2024 12:00:0 AM”
“2024-09-14”

Hi Bruce,
Thanks for the explanation, Am I right to assume that as long as I use my code on machines that have the same date/time settings as me, that my code should work? I understand that to code for all eventualities it would be better to use Adam’s example.
Nik

No. Try this. All I’ve changed is the day of the month in the default answer:

set user_date to text returned of (display dialog "enter start date" default answer "28/09/07")
set new_date to (date user_date) + 7 * days
set new_short_date to (short date string of (new_date))

Sorry Adam,
I’m not sure what you’re getting at, when I use the code you just posted it’s returning what I would expect “5/10/07”
Thanks,
Nik

On my machine, it errors with an invalid date and time.

Yes. And if you’re only writing for yourself and are never going to change your own settings, that should be good enough. Otherwise, depending on what the script’s for, you’ll either have to impose a short-date format and write code that can use and produce that regardless of the machine, or allow the machine’s own format to be used.

If you want to use the latter approach, you could use ‘short date string’ to format the default answer in the dialog instead of using a pre-formatted value in the script text:

set user_date to text returned of (display dialog "enter start date" default answer (short date string of («data isot323030372D30392D3238» as date)))
set new_date to (date user_date) + weeks
set new_short_date to (short date string of (new_date))

Or you could leave the default answer blank and code an explicit format into the prompt instead:

-- Compile and parse the machine's own interpretation of 'date "1/2/3"'.
set {year:y, day:d} to date ("1/2/3" as string)
set y to y mod 10 -- the units value from 2001, 2002, or 2003
set m to 6 - y - d -- the remaining value out of 1, 2, and 3
-- Use the 1, 2, 3 results to arrange a format prompt string.
tell {missing value, missing value, missing value}
	set item y to "yy"
	set item m to "mm"
	set item d to "dd"
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "/"
	set formatStr to it as text
	set AppleScript's text item delimiters to astid
end tell

set user_date to text returned of (display dialog "enter start date (" & formatStr & ")" default answer "")
set new_short_date to short date string of ((date user_date) + weeks)

Of course you could always test something like this to find out what the format was:

set D to short date string of date "Saturday, February 1, 2003 12:00:00 AM"
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set T to text items of D
set AppleScript's text item delimiters to tid
T --> {"2", "1", "03"} (on my machine)

I don’t use slashes in my short dates.

Hi guys,
Thanks for all your input, it just proves there’s always more than one way to skin a cat! The script will only be used on one or two machines that I can control the time/date formats. I’ll try playing with the your codes.
Thanks again,
Nik

Hi guys,
another question along the same lines, is ther any way of adding months? If I have text returned as “01/01/07” I would like to be able set a new date to “01/02/07”. The day part of the date string should always remain as “01” but the month and year part of the date string needs to change so when I get to a date of “01/12/07” the next time the repeat runs it knows that the next date should be “01/01/08” hope this makes sence!!!
Thanks,
Nik

Hi guys,
I’ve come up with this as a workaround

set Start_date to text returned of (display dialog "Enter Start Date" default answer "01/12/07")
set number_of_issue to text returned of (display dialog "How many issues do you want to create" default answer "12")

set run_flag to 1
repeat with i from 1 to number_of_issue
	if run_flag > 1 then
		set new_date to (date Start_date) + 5 * weeks
		set new_short_date to (short date string of (new_date))
	else
		set new_short_date to Start_date
	end if
	
	if character 2 of new_short_date = "/" then
		set new_short_date to "01" & characters 2 thru end of new_short_date
	end if
	if character 5 of new_short_date = "/" then
		set new_short_date to characters 1 thru 3 of new_short_date & "0" & characters 4 thru end of new_short_date as string
	end if
	set Start_date to new_short_date
	set run_flag to run_flag + 1
	display dialog Start_date
end repeat

I’m sure ther must be a simpler or more practical way of doing this?
Thanks,
Nik

If you’re using Tiger, you can simply add 1 to the date’s month; otherwise you’ll need some more involved scripting. You can add 1 to the year on any system. Don’t forget, though, that some dates don’t exist in the following month or year. If you add one to the month of 31st January, for instance, the result will overflow to the 2nd or 3rd March.

set user_date to text returned of (display dialog "enter start date" default answer (short date string of («data isot323030372D30392D3238» as date)))
set user_date to (date user_date)

copy user_date to new_date -- Duplicate the date object.
set new_date's month to (new_date's month) + 1 -- Add one to the month. (Tiger “ and presumably Leopard “ only.)
set new_date's year to (new_date's year) + 1 -- Add one to the year.

-- Check for date overflow. If it's occurred, set the new date to the last day of the target month.
set new_day to new_date's day
if (new_day < user_date's day) then set new_date to new_date - new_day * days

set new_short_date to (short date string of (new_date))

Hi Nigel,
Great examples. Although my script gives me the result I wanted I wasn’t happy that it was a compromise for not knowing the correct way to process dates.
Thanks very much,
Nik