- Offizieller Beitrag
Folgendes ist bei einem Projekt von mit angefallen:
Der Nutzer soll vollkommen frei und für ihn verständlich Tagesdatum/Zeit oder beliebige Elemente daraus formatieren können.
Folgende Makros stehen zur Verfügung:
YYYY / YY - Jahr 4/2 -stellig
MM / M - Monat 2/1 -stellig
DD / D - Tag 2/1 -stellig
hh / h - Stunde 2/1 -stellig
mm / m - Minute 2/1 -stellig
ss / s - Sekunde 2/1 -stellig
ww / w - Wochentag lang/kurz (Montag/Mo)
Bsp. "YYYY-MM-DD", "YYYYMMDD", "D.M.YY", "ww, DD.MM.YYYY", "DD.MM.YYYY hh:mm:ss" (Standard)
Einstellig bedeutet, wenn einstelliger Wert, wird er auch einstellig ausgegeben. Zweistellig fügt dann eine Vornull an.
Optional gibt es die Möglichkeit einen String vorne und/oder hinten anzufügen.
Die vom User gewünschte Formatierung kann z.B. aus einer INI eingelesen und von diesem intuitiv angepasst werden.
Spoiler anzeigen
;===============================================================================
; Function Name....: _DateTimeUser
; Description......: Nutzergesteuerte Datum- und/oder Zeitausgabe
; Parameter(s).....: $_time_format Nutzerdefiniertes Ausgabeformat
; .................: Makros YYYY / YY - Jahr 4/2 -stellig
; .................: MM / M - Monat 2/1 -stellig
; .................: DD / D - Tag 2/1 -stellig
; .................: hh / h - Stunde 2/1 -stellig
; .................: mm / m - Minute 2/1 -stellig
; .................: ss / s - Sekunde 2/1 -stellig
; .................: ww / w - Wochentag lang/kurz (Montag/Mo)
; .................: Bsp. "YYYY-MM-DD", "YYYYMMDD", "D.M.YY", "DD.MM.YYYY hh:mm:ss" (Standard)
; .................: "ww, DD.MM.YYYY"
; .................: Auch einzelne Werte und beliebige Kombinationen können abgefragt werden. "DD-hh-mm"
; ....[optional]...: $_pre vorangestellter String
; ....[optional]...: $_post nachgestellter String
; Return Value(s)..: Der formatierte Datum/Zeitstring für das heutige Datum
; Author(s)........: BugFix ( [email='bugfix@autoit.de'][/email] )
;===============================================================================
Func _DateTimeUser($_time_format='', $_pre='', $_post='')
$_time_format = ($_time_format = '') ? 'DD.MM.YYYY hh:mm:ss' : $_time_format
Local $aTime[10][2] = [['MM', @MON], ['DD', @MDAY], ['hh', @HOUR], ['mm', @MIN], ['ss', @SEC], _
['M', @MON], ['D', @MDAY], ['h', @HOUR], ['m', @MIN], ['s', @SEC]]
StringReplace($_time_format, 'Y', 'Y')
Local $sYear = StringLeft('YYYY', @extended), $output = $_time_format
Local $srepl = (StringLen($sYear) < 4) ? StringRight(@YEAR, 2) : @YEAR
If $sYear <> '' Then _
$output = StringReplace($_time_format, $sYear, $srepl)
Local $sformat
For $i = 0 To 9
$sformat = '%0' & StringLen($aTime[$i][0]) & 'd'
$output = StringReplace($output, $aTime[$i][0], StringFormat($sformat, $aTime[$i][1]), 1, 1)
Next
If StringInStr($output, 'w') Then
Local $YY = StringRight(@YEAR, 2), $aMonval[13] = ['',0,3,3,6,1,4,6,2,5,0,3,5], $iDiff
Local $Y_val = Mod(($YY + Int($YY/4)), 7), $M_val = $aMonval[@MON], $D_val = @MDAY
Local $aY_val[7][2] = [[2300,0],[2200,2],[2100,4],[2000,6],[1900,0],[1800,2],[1700,4]]
For $i = 0 To 6
If @YEAR >= $aY_val[$i][0] Then
$iDiff = $aY_val[$i][1]
ExitLoop
EndIf
Next
If ((Mod(@YEAR,400)=0)?1:(Mod(@YEAR,100)=0)?0:(Mod(@YEAR,4)=0)?1:0) Then $iDiff -= 1
Local $aWDay[7][2] = [['Sonntag','So'],['Montag','Mo'],['Dienstag','Di'],['Mittwoch','Mi'], _
['Donnerstag','Do'],['Freitag','Fr'],['Samstag','Sa']]
Local $w = Mod($Y_val + $M_val + $D_val + $iDiff, 7)
$output = StringReplace($output, 'ww', $aWDay[$w][0], 1, 1)
$output = StringReplace($output, 'w', $aWDay[$w][1], 1, 1)
EndIf
Return $_pre & $output & $_post
EndFunc ;==>_DateTimeUser
Die Wochentags- und Schaltjahrermittlung habe ich bewußt ohne externe Funktionen berechnet, damit diese Funktion auch ohne Includes auskommt.