![]() ![]() ISO8601StringToDate = DateSerial(ISO8601Split(0), ISO8601Split(1), ISO8601Split(2)) 'DateSerial returns a real numeric date ISO8601Split = Split(ISO8601String, "-") 'split input yyyy-mm-dd by dashes into an array with 3 parts Private Function ISO8601StringToDate(ByVal ISO8601String As String) As Date ' convert yyyy-mm-dd string into numeric date OldDateAge = RetVal 'return the age as result of the function 'subtract date1 from date2 and divide by 365 to get years, then round down to full years to respect the birthday date. RetVal = WorksheetFunction.RoundDown((localDate2 - localDate1) / 365, 0) Public Function OldDateAge(ByVal Date1 As Variant, ByVal Date2 As Variant) As Long OldDateDiff = RetVal 'return the difference as result of the function RetVal = DateDiff(Interval, localDate1, localDate2) 'calculate the difference between dates with the desired interaval eg yyyy for years If localDate1 0 And localDate2 0 Then 'make sure both dates were filled with values If VarType(Date2) = vbDate Or VarType(Date2) = vbDouble Then LocalDate1 = ISO8601StringToDate(Date1) 'if it is a string convert it to numericĮlse 'neither string nor numeric throw an errorĭim localDate2 As Date 'same as for Date1 but with Date2 LocalDate1 = CDate(Date1) 'if numeric take itĮlseIf VarType(Date1) = vbString Then 'check if Date1 is a string If VarType(Date1) = vbDate Or VarType(Date1) = vbDouble Then 'check if Date1 is numeric Public Function OldDateDiff(ByVal Date1 As Variant, ByVal Date2 As Variant, ByVal Interval As String) As Longĭim RetVal As Long 'variable for the value we want to return
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |