It seems dateKLAVDef
is always in dd-MM-yyyy hh-mm-ss. Is that correct? Then you have to retrieve the day, month and year from this string and make it a Date
with DateSerial(y, m, d)
. It will automatically transform to the correct locale formatting. Do a DateDiff
with the current date et voila, you are ready:
' Read date/time value from an application in registry - will
' return e.g. 05-01-2013 07-19-00
dateKLAVDef = "05-01-2013 07-19-00"
' Define a pattern for the given string
dateKLAVDefPattern = "^(\d+)-(\d+)-(\d+) .*"
' Make a regular expression
Set re = new RegExp
re.Pattern = dateKLAVDefPattern
' Execute the regex
Set matches = re.Execute(dateKLAVDef)
' Retrieve the submatches from the match
dateKLAVDefDay = matches(0).submatches(0)
dateKLAVDefMonth = matches(0).submatches(1)
dateKLAVDefYear = matches(0).submatches(2)
' recreate the dateKLAVDef date as a real date object
dateKLAVDefAsDate = DateSerial(dateKLAVDefYear, dateKLAVDefMonth, dateKLAVDefDay)
' Do a datediff on the created date and the current date
iAVDefAge = DateDiff("d", dateKLAVDefAsDate, Date())
MsgBox iAVDefAge