Doing Time Offsets of HH/MM/SS and getting the MM/DD/YYYY/HH/MM/SS right

I'm not sure I understand what you mean.
Doesn't the Numbers DATEVALUE just convert a date string to a Numbers Date?
That is all that I am doing.
In one formula I am building the string to be converted to Date.

Once you have a Numbers Date, can you can add a number to it, where the number is in units of "days"?

According to How To Add Time In Excel Or Numbers, both Excel and Numbers do date arithmetic in units of days.

So, it is easy enough to add HH:MM:SS to a date: just convert everything to units of days:
timeToAdd (days) = (((sec / 60) + min) / 60) + hrs) / 24
newDate = currentDate + daysToAdd

You could add this to the Excel forumlas:

You are correct. I misread it because I know excel can convert time into an epoch and the you add the time change.

Your suggestion works great!

1 Like

If you only want to convert the string format and the time zone, you can also do it in one go, without Epoch seconds and without calculations:

datestringOne="2016-05-01 10:02:32 -0500"

# To your local time
date -j -f "%Y-%m-%d %T %z" "$datestringOne" +"%D %T %Z"

# To a specific time zone
TZ="Europe/Lisbon" date -j -f "%Y-%m-%d %T %z" "$datestringOne" +"%D %T %Z"

2016-05-01 10:02:32 -0500 β†’ 05/01/16 16:02:32 WEST

Apparently this also takes DST into account.

2 Likes

Very nice, @Tom.
Another one for my new snippets manager, SnippetsLab.

1 Like

The Satimage.osax AppleScript Extension will do similar magic:

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/02/12 22:40
# dMod: 2017/02/12 22:55 
# Appl: AppleScript
# Task: Convert a Date-String to an AppleScript and Produce a Formatted Date-String.
# Libs: None
# Osax: Satimage.osax
# Tags: @Applescript, @Script, @Satimage.osax, @Dates, @Formatting
------------------------------------------------------------------------------

##  Requires the Satimage.osax to be installed.  ##

set dateStr to "2016-05-01 10:02:32 -0500" --> CST with daylight savings

set asDate to strptime dateStr encoding "%Y-%m-%d %H:%M:%S %z"
--> date "Sunday, May 01, 2016 at 10:02:32"

set newDateStr to strftime asDate into "%m-%d-%Y %H:%M"
--> "05-01-2016 10:02" --> Local Time

set newDateStr to strftime asDate into "%m-%d-%Y %H:%M" with GMT
--> "05-01-2016 15:02" --> Zulu Time

------------------------------------------------------------------------------

The shell's date command has more overall options, but I can usually do what I want with a liittle AppleScript and the Satimage.osax. It's faster and usually a bit easier.

-Chris

1 Like

Satimage is a very longstanding scripting addition, and it is still seeing regular updates, the last one March 2016.

However, I became a bit reluctant when it comes to third-party additions (except with Perl :flushed:).

For example, during the last years I generously made use of Shane Stanley’s ASObjC Runner, mainly as vanilla scripting addition, not so much as ASObjC helper.

With macOS Sierra ASObjC Runner definitely ceased to work. Since I also used it in some longish and complex production scripts (where I – admittedly – almost couldn’t read my own code anymore), the necessary rewrites caused some days of very unpleasant work (in spite of BridgePlus).

Of course, this consideration is completely irrelevant for short-term scripts, and probably not so relevant for simple scripts for personal use.

[quote="Tom, post:13, topic:6308"]
Satimage is a very longstanding scripting addition, and it is still seeing regular updates, the last one March 2016.[/quote]

Hey Tom,

Much more recent if you watch the Smile beta page.

http://satimage.fr/software/en/downloads/downloads_nextsmile.html

The Satimage.osax has been well maintained since 2003 (I've happily used it and evangelized it for 14+ years now).

That's nearly as long as OSX/macOS has existed and a significantly longer lifespan than some of Apple's own software.

While there will be some point somewhere down the road when it is no longer viable, I'm confident that won't be anytime soon.

-Chris

1 Like

@Tom, I'm with Chris (@ccstone) on this one. I've been using Satimage.osax for about 5 years now, and have never experienced an issue. I'm still on El Capitan, but with users like Chris who are running Sierra, I have no reason to doubt Satimage.osax will continue to work for quite a while.

Having said that, I'm in the process of moving most of my scripting from AppleScript to JXA, where I don't need most Scripting Additions. JavaScript provides native functions for most of the stuff I need from Satimage.osax, like RegEx, array/list/object manipulation, string functions, date functions, match functions, and more.

1 Like

I also think that’s a very good thing to do. It already was my new year’s resolution to finally get into JS, for the same purpose. But, you know, there is always something else more pressing to do :expressionless:

So, thanks for your reminder! I will clip it to Quiver, with a todo tag :slightly_smiling_face:

Dates and times in AppleScript are fairly straightforward. It is rather flexible about what it will coerce to a date, but the results depend on your settings in International System Preferences. To enter a date and time, the following works well for US systems:

theDate = date(β€œmm/dd/yyyy”) + hhhours + mmminutes + ss

where

mm = month
dd = day
yyyy = 2 or 4 digit year
hh = hour
mm = minute
nn = second

AppleScript measures and stores time in seconds. This is why the ss value isn’t multiplied by any term. The AppleScript terms β€œminutes” and β€œhours” represent the number of seconds in a minute or an hour. Likewise the term β€œdays”, β€œweeks”, or β€œmonths” represent the number of seconds in a day, a week or a month.

to add β€œn” seconds, minutes, hours, days, etc:

  • n
  • n * minutes
  • n * hours
  • n * days
    etc.

AppleScript takes care of any rollover; hence if an addition or subtraction goes beyond the end/start of a day, month or year, the answer will be correct.

More information at: http://macscripter.net/viewtopic.php?id=24737&p=1

Regards,

Eric

Please note that if you intend to share this, this will not work well internationally. If I use this format in Australia, it needs to be date("dd/mm/yyyy").

I don't see any obvious way to get an internationally safe date in AppleScript, but I am sure there must be.

Peter, I'm sure there are a number of solutions that will work. Here is one that works for me.
(I'm sure @ccstone will have a better solution. :wink: )

When I want date input from the user that will work in all time zones, all locals, I use the ISO 8601 format:
yyyy-mm-dd (can use any delimiter)

Once you have a valid AppleScript date object, you can do the math @gaseous1 described above.

Having said all that, I would probably use the KM tools (tokens/functions) to do date math. See Dates and Times [Keyboard Maestro Wiki].

But if anyone wants to use AppleScript, here is my solution:

##applescript Date Handling

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--- ENTER THE DATE IN THE FORMAT THAT IS SET IN YOUR SYSTEM PREFERENCES ---
set dateInYourLocale to convertToDate("02/01/2017")

--- INTERNATIONAL DATE FORMAT ---
set dateISO to convertToDate("2017-01-02")

log ("dateInYourLocale: " & dateInYourLocale as text)
log ("dateISO: " & dateISO as text)

--- NOW TRY INVALID DATE FORMAT FOR U.S. ---
--    (but should work for Australia)
set dateAus to convertToDate("31/01/2017")
--> error


### RESULTS ###
(*dateInYourLocale: Wed, Feb 1, 2017 at 12:00:00 AM*)
(*dateISO: Mon, Jan 2, 2017 at 12:00:00 AM*)
(*
[ERROR]
INVALID Date Format: 31/01/2017
*)

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--    HANDLERS (functions)
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on convertToDate(pDate) -- @Date @Convert @String
  (*  VER: 1.1    2017-03-02
  ---------------------------------------------------------------------------------
    PURPOSE:  Convert Date String to AppleScript Date Type
    PARAMETERS:
      β€’ pDate    | text OR date  | Date to check/convert
    RETURNS:  date β”‚  AppleScript date    
    AUTHOR:  JMichaelTX
    β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
  *)
  
  -- MUST NOT BE IN ANY APP WHEN USING THE date FUNCTION --
  
  if (class of pDate = date) then
    set dateAsDate to pDate
    
  else -- Convert date string
    
    try -- Try Standard U.S. date format --
      set dateAsDate to date (pDate)
    on error --- Try International Date format (YYYY-MM-DD)
      set dateAsDate to my convertISOtoDate(pDate)
    end try
    
  end if
  
  return dateAsDate
  
end convertToDate
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on convertISOtoDate(pDateStr) -- @Date @ISO @String
  (*  VER: 1.1    2017-03-02
---------------------------------------------------------------------------------
  PURPOSE:  Convert Date String in ISO format to AppleScript Date Type
  PARAMETERS:
    β€’ pDateStr    | text | Date String in ISO format
  RETURNS:  date β”‚  AppleScript date    
  AUTHOR:  JMichaelTX
  β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
*)
  
  --- pDateStr MUST be in the format of YYYY<delim>MM<delim>DD
  ---    where <delim> can be any character
  ---    like 2016-01-05
  
  set resultDate to the current date
  
  try
    
    set the year of resultDate to (text 1 thru 4 of pDateStr)
    set the month of resultDate to (text 6 thru 7 of pDateStr)
    set the day of resultDate to (text 9 thru 10 of pDateStr)
    set the time of resultDate to 0
    
    if (length of pDateStr) > 10 then
      set the hours of resultDate to (text 12 thru 13 of pDateStr)
      set the minutes of resultDate to (text 15 thru 16 of pDateStr)
      
      if (length of pDateStr) > 16 then
        set the seconds of resultDate to (text 18 thru 19 of pDateStr)
      end if
    end if
    
  on error errMsg number errNum
    error "[ERROR]" & return & "INVALID Date Format: " & pDateStr
    
  end try
  
  return resultDate
  
end convertISOtoDate
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

###My macOS Preferences (for reference)

1 Like

You can get the date/time in ISO format with this:

set theDate to (current date) as Β«class isotΒ» as string

β†’ 2017-03-04T01:33:05

Yes, but you cannot seem to do the reverse of that.

Apparently in older versions of the system, you could do something like:

date ("2017-03-04T11:15:01" as Β«class isotΒ»)

But that no longer works.

Without the β€œT” it works:

date ("2017-03-04 11:15:01")

β†’ date "Saturday, 4 March 2017 at 11:15:01"
β€Œ

So, from localized current date β†’ ISO date β†’ localized date:

set AppleScript's text item delimiters to {"T"}
set isoDate to text items of ((current date) as Β«class isotΒ» as string)
set AppleScript's text item delimiters to {" "}
set localizedDate to date (isoDate as text)

Hmmm, that doesn't work for me:

Running Script Editor 2.8.1 (183.1) on macOS 10.11.6


That doesn't work either, this time in Script Debugger:

Script Debugger 6.0.4 (6A198) on macOS 10.11.6.

@Tom, it must be something in your system that allows that?

1 Like

Hmm. Works in both ScriptDebugger 6.0.4 (6A198) and in Script Editor 2.9 (191); macOS 10.12.3 (16D32).

Removed all .osax from /Library/ScriptingAdditions and ~/Library/ScriptingAdditions, restarted the Mac, and it still works.

Tom, I just tried it on a different Mac, but with same result, same error.

So, it must be how AppleScript is handling localization of dates -- that's all I can think of.

I'll post on the AppleScript Users List, and see what people report.

Yep, on my other Mac (also 10.12.3) I get the same error as you.

Have to see what this is…

@Tom, this just reported from the ASUL:

Works fine for me on 10.12.3:

date "Saturday, March 4, 2017 at 11:15:01"

My short date pref is set to yyyy/MM/dd instead of the standard MM/dd/yyyy, if that makes any difference.