Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 03-16-2013, 08:23 AM   #1
hisara
Major Leaguer
 
Join Date: Mar 2006
Posts: 280
Various dates to date

I have lots of dates like these:
July 7, 2010
7. 31. 2011
tuesday, june 10th, 2010
...

My AppleScript needs to be able to convert date, which is written in any format to yymmdd format.

How i can do this? Thanks
hisara is offline   Reply With Quote
Old 03-16-2013, 10:24 AM   #2
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,045
PHP has a strtotime() function that works very well - you could set something up with a `do shell script ... ` line.
acme.mail.order is offline   Reply With Quote
Old 03-16-2013, 11:17 AM   #3
hisara
Major Leaguer
 
Join Date: Mar 2006
Posts: 280
Quote:
Originally Posted by acme.mail.order
PHP has a strtotime() function that works very well - you could set something up with a `do shell script ... ` line.

I did not know that PHP can be run like this. I know nothing about PHP.

This works, but how i need to change strtotime row, so it converts dates?
do shell script "php -q " & quoted form of POSIX path of "/Users/Hisara/Desktop/date.txt"

date.txt:
<?php
date_default_timezone_set('Europe/London');

echo strtotime("now"), "\n";
?>

And how i can have multiple lines of code without using external txt file? This fails:

do shell script "php -q date_default_timezone_set('Europe/London');
echo strtotime("now"), "\n";"

Thanks
hisara is offline   Reply With Quote
Old 03-16-2013, 07:00 PM   #4
NovaScotian
Hall of Famer
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 4,945
If you'd prefer plain vanilla AppleScript then this works if the dates don't include things like "10th" or "3rd". Of course those could be filtered out if your dates do:

Code:
set tDates to {"July 7, 2010", "7. 31. 2011", "tuesday, june 10, 2010"}
set Reslt to {}
repeat with aDate in tDates
	set someDate to date aDate
	set {year:y, month:m, day:d} to (someDate)
	set end of Reslt to text 3 thru -1 of ((y * 10000 + m * 100 + d) as string)
end repeat
--> {"100707", "110731", "100610"}
__________________
17" MBP, OS X 10.8.3; 27" iMac, OS X 10.8.3
NovaScotian is offline   Reply With Quote
Old 03-16-2013, 07:58 PM   #5
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,045
Quote:
Originally Posted by hisara
I did not know that PHP can be run like this. I know nothing about PHP.

This works, but how i need to change strtotime row, so it converts dates?
do shell script "php -q " & quoted form of POSIX path of "/Users/Hisara/Desktop/date.txt"

If you want to make a batch file just have PHP read the file directly. fopen() or file_get_contents();

If you prefer to have (almost) everything done via Applescript just put the entire php command on one line. Build it as a string.

Quote:
echo strtotime("now"), "\n";

That will give you a unixtime integer. Explore the date() function. The line break is optional.
acme.mail.order is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 03:49 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.