AppleScript: Letzter Tag des Vormonats

thowi

thowi

Aktives Mitglied
Thread Starter
Dabei seit
14.10.2007
Beiträge
560
Reaktionspunkte
33
Hallo zusammen,

ich brauche in einem AppleScript den letzten Tag des Vormonats. Wenn ich das Skript also heute ausführe, soll als Ergebnis "2022-02-28" herauskommen.
Wie kann ich diesen text erhalten?

Ich brauche natürlich auch den 1. Tag des Vormonats - das hab ich aber bereits gelöst:
AppleScript:
set d to current date
set Tag to text -2 thru -1 of ("0" & d's day)
set Monat to text -2 thru -1 of ("0" & ((month of d) * 1))
set VorMonat to text -2 thru -1 of ("0" & (((month of d) - 1) * 1))
set Jahr to ((year of d) as text)
set VormonatStart to Jahr & "-" & VorMonat & "-01"
-- Quelle: http://www.fischer-bayern.de/applescript/html/datum.html
Jetzt fehlt mir noch der letzte Tag. Es wäre nett, wenn mir hier jemand weiterhelfen könnte.

Vielen Dank!
 
Viele Grüße

Code:
-- heutiger Zeitstempel
set heute to current date

-- Zeitstempel von heute kopieren und Tag zu 1, also Monatsanfang setzen
copy heute to dieserMonatAnfang
set day of dieserMonatAnfang to 1

-- Monatsanfang einen Tag abziehen ergibt den letzten Tag vom Vormonat
set VormonatEnde to dieserMonatAnfang - (1 * days)

-- Monatsende vom Vormonat zum 1. setzen ergibt Monatsanfang vom Vormonat
copy VormonatEnde to VormonatAnfang
set day of VormonatAnfang to 1

-- Vormonat Anfanf/Ende in technisches Format überführen und ausgeben
log my dateToText(VormonatAnfang)
log my dateToText(VormonatEnde)

-- Ergebnis
(*2022-02-01*)
(*2022-02-28*)

--

on dateToText(theDate)
    
    --
    
    set allItems to {}
    
    --
    
    set aText to my dateToDayText(theDate)
    
    if (length of aText < 1) then
        
        return ""
        
    end if
    
    --
    
    copy aText to end of allItems
    
    --
    
    (*
    
    set aText to my dateToTimeText(theDate)
    
    if (length of aText < 1) then
        
        return ""
        
    end if
    
    --
    
    copy aText to end of allItems
    
    --
    
    set AppleScript's text item delimiters to " "
    
    *)
    
    --
    
    set aText to allItems as text
    
    --
    
    return aText
    
    --
    
end dateToText

--

on dateToTimeText(theDate)
    
    --
    
    if (theDate is missing value) then
        
        return ""
        
    end if
    
    --
    
    set allItems to {}
    
    --
    
    set aText to hours of theDate
    
    if (aText < 10) then
        
        set aText to "0" & aText
        
    end if
    
    --
    
    copy aText to end of allItems
    
    --
    
    set aText to minutes of theDate
    
    if (aText < 10) then
        
        set aText to "0" & aText
        
    end if
    
    --
    
    copy aText to end of allItems
    
    --
    
    set aText to seconds of theDate
    
    if (aText < 10) then
        
        set aText to "0" & aText
        
    end if
    
    --
    
    copy aText to end of allItems
    
    --
    
    set AppleScript's text item delimiters to "-"
    
    --
    
    set aText to allItems as text
    
    --
    
    return aText
    
    --
    
end dateToTimeText

--

on dateToDayText(theDate)
    
    --
    
    if (theDate is missing value) then
        
        return ""
        
    end if
    
    --
    
    set allItems to {}
    
    --
    
    set aText to year of theDate
    
    if (aText is missing value) then
        
        set aText to ""
        
    end if
    
    --
    
    copy aText to end of allItems
    
    --
    
    set aText to my monthToNumber(month of theDate)
    
    if (aText < 10) then
        
        set aText to "0" & aText
        
    end if
    
    --
    
    copy aText to end of allItems
    
    --
    
    set aText to day of theDate
    
    if (aText < 10) then
        
        set aText to "0" & aText
        
    end if
    
    --
    
    copy aText to end of allItems
    
    --
    
    set AppleScript's text item delimiters to "-"
    
    --
    
    set aText to allItems as text
    
    --
    
    return aText
    
    --
    
end dateToDayText

--

on monthToNumber(theMonth)
    
    --
    
    if (theMonth is missing value) then
        
        return 0
        
    end if
    
    --
    
    if (theMonth is January) then
        
        return 1
        
    end if
    
    --
    
    if (theMonth is February) then
        
        return 2
        
    end if
    
    --
    
    if (theMonth is March) then
        
        return 3
        
    end if
    
    --
    
    if (theMonth is April) then
        
        return 4
        
    end if
    
    --
    
    if (theMonth is May) then
        
        return 5
        
    end if
    
    --   
    
    if (theMonth is June) then
        
        return 6
        
    end if
    
    --
    
    if (theMonth is July) then
        
        return 7
        
    end if
    
    --
    
    if (theMonth is August) then
        
        return 8
        
    end if
    
    --
    
    if (theMonth is September) then
        
        return 9
        
    end if
    
    --
    
    if (theMonth is October) then
        
        return 10
        
    end if
    
    --
    
    if (theMonth is November) then
        
        return 11
        
    end if
    
    --
    
    if (theMonth is December) then
        
        return 12
        
    end if
    
    --
    
    return 0
    
    --
    
end monthToNumber

--
 
  • Gefällt mir
Reaktionen: thowi
Sehr cool, vielen vielen Dank euch! @little_pixel funktioniert hervorragend!
 
Zurück
Oben Unten