Jump to content
alex.agat

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

Share this post


Link to post

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.

Share this post


Link to post

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

Share this post


Link to post

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

Share this post


Link to post

One of these days I was browsing in full screen and I needed something like that. I thought Alfred already had something like that but I forgot about it. I think you should make a workflow and share with us. :)

Share this post


Link to post

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.

Share this post


Link to post

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.

Share this post


Link to 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

 

Thanks Tyler. I thought the addition might be a cool feature for the OP to incorporate in his first workflow.  In AppleScript you can use:

 

set newDate to (current date) + 1 * days

Share this post


Link to post

Even though I am certainly more comfortable in PHP, since I started my original script in AppleScript I thought why not continue down that path.

I have updated my original script to include the +num and -num functionality requested above.

 

Here ya go: http://cl.ly/3w0O0p3Q3j0X

Edited by alex.agat

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...