1 votes

Comment formater une heure en HHMM avec AppleScript ?

Est-il possible d'obtenir l'heure et les minutes au format 24 heures HHMM, de sorte que 1:01 soit 0101 ?

J'ai trouvé un moyen de le faire pour le mois et l'année mais je ne trouve pas de moyen pour l'heure et les minutes.

Voici ce que j'ai pour l'instant.

set whichUrl to 0
set fileNames to "map"
set theStartDate to the date "Tuesday, June 21, 2016 at 00:00:00"
set theEndDate to theStartDate + (60 * 60 * 24 * 365) --1 year
set theDate to theStartDate

repeat until theDate = theEndDate
        set theURL to "http://site.com/map.php?iso="

        set {year:y, month:m, day:d} to (theDate)
        set theModDate to (y * 10000 + m * 100 + d) as string

        set theURL to theURL & theModDate & hours of theDate
        set theURL to theURL & minutes of theDate

        tell application "Safari"
            activate
            set the URL of document 1 to theURL as text
            delay 2

        end tell
    set theDate to theDate + (600) --advance 10 minutes
    set whichUrl to whichUrl + 1
end repeat

0voto

Christian Boyce Points 1052

Voici un script qui prend l'heure actuelle et la transforme en HHMM comme vous l'avez demandé.

-- preserve the current text item delimiters so we can put them back
set oldDelims to AppleScript's text item delimiters

set right_now to current date
set the_time to time string of right_now -- something like "4:20:56 PM"
set am_pm to characters -2 thru -1 of the_time as string -- is it AM or PM

set AppleScript's text item delimiters to ":"
set the_hour to text item 1 of the_time as string -- in this example, "4"
set the_minute to text item 2 of the_time as string -- in this example, "20"

if (am_pm is "PM") then
    set the_hour to the_hour + 12
end if

if the_hour is "24" then
    set the_hour to "12"
end if
-- that could have been more graceful but it's nice and obvious this way

if (count of the_hour) is 1 then -- padding with a zero if necessary
    set the_hour to "0" & the_hour
end if

if (count of the_minute) is 1 then -- padding with a zero if necessary
    set the_minute to "0" & the_minute
end if

set AppleScript's text item delimiters to ""
set steves_string to the_hour & the_minute as string -- putting the pieces together

set AppleScript's text item delimiters to oldDelims -- cleaning up

return steves_string -- you would not need this line, but for testing it's nice

Je suis sûr que cela pourrait être renforcé, mais j'ai pensé qu'il était instructif de le voir étape par étape.

Vous pouvez certainement indiquer l'heure que vous voulez - il n'est pas nécessaire que ce soit la "date du jour". (Notez que la date actuelle comprend également l'heure. Vous enlevez la partie "heure" d'une date en demandant "la chaîne de temps" de celle-ci.

0voto

Steve Torrence Points 165
set whichUrl to 0
set theStartDate to the date "Tuesday, June 21, 2016 at 00:00:00"
set theEndDate to theStartDate + (60 * 60 * 24 * 365) --365 normally
set theDate to theStartDate

repeat until theDate  theEndDate
    if whichUrl  19997 and whichUrl  40000 then --In case I just want a range.
        set theURL to "http://siteaddress?iso="

        set theModDate to theDate

        set theModDate to theModDate as date
        set D to characters -2 through -1 of ("0" & theModDate's day) as text
        copy theModDate to tempDate
        set the month of tempDate to January
        set M to characters -2 thru -1 of ("0" & 1 + (theModDate - tempDate + 1314864) div 2629728) as text
        set Y to characters -1 thru -4 of ((year of theModDate) as text) as text
        set hh to "0" & theModDate's hours as text
        set hh to characters ((count of hh) - 1) through (count of hh) of hh
        set mm to "0" & theModDate's minutes as text
        set mm to characters ((count of mm) - 1) through (count of mm) of mm
        set ss to "0" & theModDate's seconds as text
        set ss to characters ((count of ss) - 1) through (count of ss) of ss
        set theDateTxt to (Y & M & D & "T" & hh & mm as text)
        set theURL to theURL & theDateTxt
        set AppleScript's text item delimiters to ","
        do shell script "curl -o ~/test/test.php." & theDateTxt & ".jpg 'http://siteaddress?iso='" & theDateTxt
        set AppleScript's text item delimiters to ""
    end if
    set theDate to theDate + (900) --seconds (1800 is 30 minutes)
    set whichUrl to whichUrl + 1
end repeat

LesApples.com

LesApples est une communauté de Apple où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres utilisateurs d'appareils Apple, poser vos propres questions ou résoudre celles des autres.

Powered by:

X