Jump to content

Basic - Display Date and Time


Recommended Posts

Hi all,

 

Just downloaded Alfred!

 

I want to create the following / know if its possible.

 

1. Type Alfred shortcut

2. Type 'datetime' as a shortcut

3. The workflow automatically calculates the current datetime (hopefully something I can control the formatting of on the backend) and displays it in the alfred hover window.

4. I have the option of going full screen (if this step though has to happen automatically it would be fine)

 

Any help is appreciated!! Thanks

 

Alex

Link to comment

Playing around, even getting it to show in a notification would be acceptable (but not ideal).

 

So far I have:

 

 

on alfred_script(q)
  return "" + (current date) + ""
end alfred_script
 
with a notification displaying the result, but it's blank.
Link to comment

Got it for posting to a notification - now its just displaying full screen using command+L

 

--

 

 

set today to current date

set dateAsString to "" as string

set wd to (weekday of today) as string

set m to (month of today) as string

set dm to (day of today) as string

set dm_int to (day of today) as integer

set dm_suffix to "" as string

set y to (year of today) as string

 

if (dm_int = 1) or (dm_int = 21) or (dm_int = 31) then

set dm_suffix to "st"

else if (dm_int = 2) or (dm_int = 22) then

set dm_suffix to "nd"

else if (dm_int = 3) or (dm_int = 23) then

set dm_suffix to "rd"

else

set dm_suffix to "th"

end if

 

set dateAsString to dateAsString & wd

set dateAsString to dateAsString & " "

set dateAsString to dateAsString & m

set dateAsString to dateAsString & " "

set dateAsString to dateAsString & dm

set dateAsString to dateAsString & dm_suffix

set dateAsString to dateAsString & ", "

set dateAsString to dateAsString & y

 

 

return dateAsString

Link to comment

Got it for posting to a notification - now its just displaying full screen using command+L

 

--

 

 

set today to current date

set dateAsString to "" as string

set wd to (weekday of today) as string

set m to (month of today) as string

set dm to (day of today) as string

set dm_int to (day of today) as integer

set dm_suffix to "" as string

set y to (year of today) as string

 

if (dm_int = 1) or (dm_int = 21) or (dm_int = 31) then

set dm_suffix to "st"

else if (dm_int = 2) or (dm_int = 22) then

set dm_suffix to "nd"

else if (dm_int = 3) or (dm_int = 23) then

set dm_suffix to "rd"

else

set dm_suffix to "th"

end if

 

set dateAsString to dateAsString & wd

set dateAsString to dateAsString & " "

set dateAsString to dateAsString & m

set dateAsString to dateAsString & " "

set dateAsString to dateAsString & dm

set dateAsString to dateAsString & dm_suffix

set dateAsString to dateAsString & ", "

set dateAsString to dateAsString & y

 

 

return dateAsString

 

Add a Large Type item to the end of the workflow instead of the Post Notification item. That should do what you are looking for

Link to comment

Will work on tonight and post

 

 

I use Date Calculator by iEnno; it supports adding/subtracting units of time (days, hours, minutes, months, etc.).

 

I hooked the script up to a 'Large Type' output. Here's a download with my edits

 

Not sure about other scripting languages but thought I would mention to Tyler since I know he's a PHP nerd like me but, PHP makes doing things like this extremely easy. The PHP function strtotime() will allow you to say current date +/- <some string> and it will return a timestamp to that date/time which could obviously then be converted to a date/time using the date command in PHP. Also, worth noting that, when your dealing with date/time in PHP, it's going to bark at you for not setting the timezone. You could always set a default but then it doesn't work correctly when you distribute to people in other timezones so to fix that, use the PHP exec command to do 

systemsetup -gettimezone

 

to get back a string of the current machines timezone like this:

Time Zone: America/Chicago

 

You could then parse that to grab everything after the Time Zone: and set that string to the new timezone using date_default_timezone_set()

 

Sorry, I got way down a rabbit hole on that one and probably gave a lot more info than anyone cared about.

Link to comment

Not sure about other scripting languages but thought I would mention to Tyler since I know he's a PHP nerd like me but, PHP makes doing things like this extremely easy. The PHP function strtotime() will allow you to say current date +/- <some string> and it will return a timestamp to that date/time which could obviously then be converted to a date/time using the date command in PHP. Also, worth noting that, when your dealing with date/time in PHP, it's going to bark at you for not setting the timezone. You could always set a default but then it doesn't work correctly when you distribute to people in other timezones so to fix that, use the PHP exec command to do 

systemsetup -gettimezone

 

to get back a string of the current machines timezone like this:

Time Zone: America/Chicago

 

You could then parse that to grab everything after the Time Zone: and set that string to the new timezone using date_default_timezone_set()

 

Sorry, I got way down a rabbit hole on that one and probably gave a lot more info than anyone cared about.

 

I had never seen strtotime() before (looks awesome); thanks for that  :)

 

It should be noted, though, that every time I tried to use the systemsetup -gettimezone command (both from bash and php exec()), I was denied because my user account does not have administrator access  :(. I know I could use sudo, but I don't want to type Administrator creds just to know the date.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...