time - setting current date and time

set theDate to current date

The above shows the current time and date. How do you change the date but keep the valid date format.

thanks

Hi slashdot,

There are many ways to change the ‘date’ (and time). What part of the date would you like to change?

Interesting stuff date and note that the AppleSciptLnaguageGuide.pdf has a lot of info on that.

gl,
kel

Firstly thank you for the reply.

I would like to know how to manipulate it all.

date “Sunday, September 1, 2013 1:50:25 PM”

change the
date
month
date
year
time

just can’t seem google anything conclusion that shows this
found - http://macscripter.net/viewtopic.php?id=24737
But it only shows how to get specific parts of the date/time not set it

ex.
if i want to set
date “Sunday, September 1, 2013 1:50:25 PM”

to

“Monday, September 2, 2013 12:34:34 AM”

From the AppleScriptLanguageGuide.pdf:

Note that the day property is read/write, so you can change that. Here’s an example:

set cur_date to (current date)
set day of cur_date to 15
return cur_date

This script will cahnge the day of the current date to the 15th.

thanks!

how would you manipulate the time?

The further away from the mass, time slows down. :slight_smile:

You should search this site if you want to learn about time. Also, download and read the AppleScriptLanguageGuide.pdf. Look at the time property. Your question is too vast.

gl,
kel

well specifically

HH
MM
SS

I am concurrently search :slight_smile:

Hi slashdot,

I thought of a good example. This script gives you a target date:

set cur_date to (current date)
set target_date to cur_date + 5 * minutes
return target_date

It’s pretty much self explanatory.

gl,
kel

set cur_date to (current date)
set target_date to cur_date + 1 * hours + 5 * minutes + 3 -- seconds
return target_date

thanks for the direction:

set cur_date to (current date)

– Date
set month of cur_date to 1
set day of cur_date to 1
set year of cur_date to 2014

– Time
set hours of cur_date to 13
set minutes of cur_date to 34
set seconds of cur_date to 33
return cur_date

No, you don’t need to do that if you want to set a specific date and time.

what do you recommend?

Something like this:

set date_as_string to "1/1/2014 1:34:33 PM"
return date date_as_string

Remember that international dates differ.

Hi slashdot,

Here’s a function for setting a date:

on GetDate(mo, d, y, h, m, s)
	if (h div 12) > 0 then
		set ampm to "PM"
	else
		set ampm to "AM"
	end if
	set h to h mod 12
	set date_string to (mo & "/" & d & "/" & y & space & h & ":" & m & ":" & s & space & ampm) as string
	return date date_string
end GetDate

GetDate(1, 1, 2014, 13, 34, 33)

I didn’t add error checking.

Which is why going via strings is a really, really bad idea. Setting the components is much safer, and probably significantly faster.

Hi Shane,

I was trying to answer the question. Don’t know what went wrong. In fact, I can’t remember the question.:slight_smile:

gl,
kel

Your entire post is completely off-topic. Please delete it.

That’s the currently accepted way to set a date to a specific value if the script has to be portable and the way you’ve written it is fine if the target date’s 1st January. But there’s a “gotcha” with certain combinations of current date and target date.

If the current date’s ‘day’ is more than the number of days in the target month, setting the month first will cause an overflow into the month after the target month. So if today’s 31st January and the target date’s 5th April, setting the month first gives 31st April, which is interpreted as 1st May. Setting the day after that gives 5th May instead of the desired 5th April.

There’s a similar trap when the day’s set first instead. If the current date’s 5th April and the target date’s 31st January, settting the day first gives 31st April (ie. 1st May) and setting the month then gives 1st January instead of 31st January.

Taking into account too the possibilities with leap and non-leap years, the safest generic approach appears to be to set the day to a number less than 29 first ” say 1, for clarity ” then set in order the year, the month, and then the required day.

set cur_date to (current date)

-- Date
set day of cur_date to 1 -- First, to ensure overflow doesn't occur during the following settings.
set year of cur_date to yr -- Second, to set the required leap or non-leap year.
set month of cur_date to mnth -- Third, to set the required month with the right length.
set day of cur_date to dy -- Last, to set the required day value.

cur_date

You could do the whole thing with a one-liner like this:

tell (current date) to set {day, year, its month, day, its hours, its minutes, its seconds, cur_date} to {1, yr, mnth, dy, hr, min, sec, it}
return cur_date

Or:

tell (current date) to set {day, year, its month, day, time, cur_date} to {1, yr, mnth, dy, hr * hours + min * minutes + sec, it}
return cur_date

Done! :slight_smile: