VBA file dates in Julian format

In VBA - Could anyone provide examples of how to get the julian dates (datelastmodified) of 2 files so those julian dates can be compared to see which file is older and newer?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Here how you convert the date of files ro julian dates:

Dim jd As Double ' julian day
  Dim mDate As Date ' your date
  mDate = Now()
 jd = mDate.ToOADate


To get date of the files:

Dim File1 As new System.IO.FileInfo

Dim strLastModified As String
strLastModified = System.IO.File.GetLastWriteTime(strFilePath & File.ToString()).ToShortDateString()
tesla764Author Commented:
I will try that 1st thing tomorrow. Thanks.
[ fanpages ]IT Services ConsultantCommented:

In Visual Basic for Applications, you don't need to convert two "Last Modified" dates to Julian Date values to compare then.

Simply format each in [yyyymmddhhmmss] Format, using the Format$() statement.

Here are two examples of usage, as well as a third method using the DateDiff() function, a fourth utilising a Function that converts the two dates into Julian Date values (as you originally requested), & a fifth simply subtracting one Date (data type) from the other...

Public Sub Q_28253825()

  Dim datLastModified1                                  As Date
  Dim datLastModified2                                  As Date
  datLastModified1 = DateValue("30/09/2013") + TimeValue("11:30:23")
  datLastModified2 = DateValue("01/10/2012") + TimeValue("02:26:14")
' First method [using Format$()]...

  If Format$(datLastModified1, "yyyymmddhhmmss") > Format$(datLastModified2, "yyyymmddhhmmss") Then
     MsgBox "datLastModified1 is later than datLastModified2 [using Format$()]"
  End If
' Second method [again, using Format$()] showing alternate approach...

  Select Case (Format$(datLastModified1, "yyyymmddhhmmss"))
      Case Is > Format$(datLastModified2, "yyyymmddhhmmss")
          MsgBox "datLastModified1 is later than datLastModified2 [also using Format$()]"
      Case Is < Format$(datLastModified2, "yyyymmddhhmmss")
          MsgBox "datLastModified1 is earlier than datLastModified2 [also using Format$()]"
      Case Else
          MsgBox "datLastModified1 is equal to datLastModified2 [also using Format$()]"
  End Select
' Third method [using DateDiff() to compare the number of seconds between each date]...

  If DateDiff("s", datLastModified2, datLastModified1) > 0& Then
     MsgBox "datLastModified1 is later than datLastModified2 [using DateDiff()]"
  End If
' Fourth method [using conversion to Julian Dates via the strJulian_Date() function]...

  If strJulian_Date(datLastModified1) > strJulian_Date(datLastModified2) Then
     MsgBox "datLastModified1 is later than datLastModified2 [using strJulian_Date()]"
  End If
' Finally, a fifth method [simply subtract one Date from the other]...

  If datLastModified1 - datLastModified2 > CDate(0&) Then
     MsgBox "datLastModified1 is later than datLastModified2 [using subtraction]"
  End If

End Sub
Function strJulian_Date(ByVal datDate As Date) As String

  strJulian_Date = Right$(Format$(Year(datDate), "0000"), 2) & _
                   Format$(DateDiff("d", DateSerial(Year(datDate), 1, 0), datDate), "000")

End Function

Open in new window



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.