florian86
Goto Top

VBA string to date

Hallo Zusammen,

kann ich einen String zum Beispiel "211022" in ein Datum konvertieren? Zum Beispiel in 22.10.21 .

MfG

Florian86

Content-Key: 1406022774

Url: https://administrator.de/contentid/1406022774

Printed on: April 28, 2024 at 11:04 o'clock

Mitglied: 149569
Solution 149569 Oct 19, 2021 updated at 12:11:58 (UTC)
Goto Top
https://www.w3schools.com/asp/func_cdate.asp
https://www.script-example.com/themen/vbscript-Datum-und-Zeit-Funktionen ...

strDate = "211022"
d = DateSerial(Left(strDate, 2), Mid(strDate, 3, 2), Right(strDate, 2))
msgbox Format(d, "dd.MM.yyyy")
Member: em-pie
em-pie Oct 19, 2021 updated at 12:19:03 (UTC)
Goto Top
Moin,

den String zerpflücken und mit CDATE() konvertieren.
Das Zerpflücken ist sinnvoll/ notwendig, da es entweder der 22.10.2021 oder der 21.10.2022 sein könnte.

dt = CDATE( RIGHT("211022", 2) & "." & LEFT( RIGHT("211022", 4), 2) & "." & LEFT("20" & "211022", 4) )  
Geht sicherlich auch eleganter...


edit:
Hier ein Beispiel:
d = "221021"  
dt = CDATE( RIGHT(d, 2) & "." & LEFT( RIGHT(d, 4), 2) & "." & LEFT("20" & d, 4) )  

wscript.echo "Day: "& RIGHT(d, 2)  
wscript.echo "Month: " & LEFT( RIGHT(d, 4), 2)  
wscript.echo "Year: " & LEFT("20" & d, 4)  

wscript.echo "Date: " & dt  
wscript.echo "Is Date? " & isdate(dt)  
wscript.echo "Add 15 Days: " & dateadd("d", 15, dt)  

Ergebnis:
Day: 21
Month: 10
Year: 2022
Date: 21.10.2022
Is Date? Wahr
Add 15 Days: 05.11.2022

Gruß
em-pie
Member: Florian86
Florian86 Oct 19, 2021 at 12:55:16 (UTC)
Goto Top
DANKE!