Page 2 of 2

Re: Convert Military Time to Standard Time?

PostPosted: Fri Nov 11, 2011 8:42 pm
by bschollnick2
ckeyes888 wrote:
Came across another issue. I have a trigger that turns on the hot tub and calculates
what time the tub will reach the desired temp based on it's current temp.
I simply take the military time and add time to it depending on the current hot tub temp.

Problem is that the new number can be outside the military time spec by going over 59 mins.
e.g. if the military time is 1550 and I add 30 mins it becomes 1580, then when I convert that
using the script above the ready time becomes 3:80 pm.


The problem here, is that this isn't a military time problem. You would have the same issue with standard time.

new_datetime = datetime.datetime.now() + datetime.timedelta (minutes=45)

So convert from 24 hour time to standard, and use the above as a template...

Re: Convert Military Time to Standard Time?

PostPosted: Sat Nov 12, 2011 12:53 am
by ckeyes888
Thanks, but I only know enough AS to get me in trouble.
Could you elaborate some on how to use that bit of code?

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Sat Nov 12, 2011 6:25 am
by bschollnick2
ckeyes888 wrote:
Thanks, but I only know enough AS to get me in trouble.
Could you elaborate some on how to use that bit of code?


Sorry, my bad, I forgot this thread was referring to Applescript. I gave you Python code...

Date-/Time-Calculations using AppleScript from http://erikslab.com/2007/11/26/date-tim ... plescript/

This short how-to explains how you calculate with time- and date-values in AppleScript. Quite a few people told me, that they are using the do shell script-handler presented in the post How to convert an “epoch”-time to a meaningful date and time. Nothing wrong with that, but if you just want to perform time-calculations inside an AppleScript, then there is an easier way.

If you assign the current time to a variable as in set myDate to current date and you just want to calculate the current date plus 2 days, then there is no need to use epoch-seconds.

set myNewDate to myDate + (2 * days), adds 2 days to the date and time stored in myDate.
set myNewDate to myDate + (4 * hours), adds 4 hours to the current time and date.
set myNewDate to myDate + (30 * minutes), adds 30 minutes to the current time and date.

It is really that simple, AppleScript uses epoch-seconds to perform these calculations, so you don’t have to.

If you need to set the date and time to the beginning of the current day, then use

set myTime to time of myDate

set myNewDate to myDate - myTime

The variable myNewDate now contains the current date and 00:00:00 as time.

Re: Convert Military Time to Standard Time?

PostPosted: Sat Nov 12, 2011 9:33 am
by ckeyes888
Thanks. Guess I'm still not getting this.

I have a time variable "ClockTime" that reads 11:18 am that I'm trying to add time to.
Been trying variations of your post above but no go yet.

set the value of variable "HotTubReadyTime" to (value of variable "ClockTime") + (30 * minutes)

The error is, "Can't make 11:18 am into type number". Makes sense as it's not just a number.

Any help?

Thanks,

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Sat Nov 12, 2011 1:48 pm
by jay (support)
Code: Select all
 set the value of variable "HotTubReadyTime" to ((date (value of variable "ClockTime")) + (30 * minutes)) as string

Re: Convert Military Time to Standard Time?

PostPosted: Sat Nov 12, 2011 2:50 pm
by ckeyes888
Thanks Jay. That works great albeit it returns "Saturday, November 12, 2011 6:46:00 PM".
Anyway to shorten that to just " 6:46 PM"?

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Sat Nov 12, 2011 5:30 pm
by jay (support)
Code: Select all
 set the value of variable "HotTubReadyTime" to time string of ((date (value of variable "ClockTime")) + (30 * minutes))


That may show seconds - if so, open your System Preferences, open the "Language & Text" preference pane, click on the "Formats" tab, click the "Customize..." button under "Times", and change the "Medium" time format - that's the one that AppleScript uses when "time string" of a date is asked for. Just remove the seconds and the extra colon after the minutes.

Re: Convert Military Time to Standard Time?

PostPosted: Sat Nov 12, 2011 5:46 pm
by ckeyes888
Actually, I was hoping for just the time, not the month date etc.
Can that be done?

Thanks,

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Sat Nov 12, 2011 7:31 pm
by jay (support)
Did you try the last one I posted?

Re: Convert Military Time to Standard Time?

PostPosted: Sat Nov 12, 2011 10:15 pm
by ckeyes888
I did. It returns the full month date and year. Works for me....just curious if it can
just be the time.

Thanks,

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Sun Nov 13, 2011 12:18 am
by jay (support)
The last one should only return "11:00:00 pm" (or something similar - it definitely should not return any of the date). Not sure why it isn't working for you. Make sure you got the last one that includes "time string" - maybe you didn't copy/paste it in correctly?

Re: Convert Military Time to Standard Time?

PostPosted: Sun Nov 13, 2011 12:11 pm
by ckeyes888
Doh! must have missed copied it. Easy to lose the seconds to just show 11:00 am?

Many thanks,

Carl

Re: Convert Military Time to Standard Time?

PostPosted: Sun Nov 13, 2011 2:58 pm
by jay (support)
Read the rest of the post that has the script in it... :P

Re: Convert Military Time to Standard Time?

PostPosted: Sun Nov 13, 2011 3:55 pm
by ckeyes888
Doh...again. I'd just set it to short. Maybe some day I'll learn to read.

Thanks,

Carl