Search code examples
ms-accessvbafilesystemobject

VBA Scipting FileSystemObject returning different time


I have an issue where I'm returning the DateCreated of a file Like this:

Set fsoFile = CreateObject("Scripting.FileSystemObject")
Set File = fsoFile.GetFile([Path] & [Filename])
debug.print File.DateCreated

and the time it's returning is an hour earlier than it says on windows explorer?

I don't know if the System time has ever been changed, or if there was ever a period of time where the system time was wrong, but it's definitely correct now.

Anyone know what this could be/had a similar issue?

Thanks


Solution

  • Could it be that you are located in Europe?

    The official documentation FILETIME structure states, that NTFS stores file dates as UTC time, thus (at present) one hour off your local time.

    You can find the current UTC time this way:

        Public Type SystemTime
            wYear                           As Integer
            wMonth                          As Integer
            wDayOfWeek                      As Integer
            wDay                            As Integer
            wHour                           As Integer
            wMinute                         As Integer
            wSecond                         As Integer
            wMilliseconds                   As Integer
        End Type
    
    ' Returns the current UTC time.
    Private Declare PtrSafe Sub GetSystemTime Lib "kernel32" ( _
        ByRef lpSystemTime As SystemTime)
    
    ' Retrieves the current date and time from the local computer as UTC.
    ' By cutting off the milliseconds, the resolution is one second to mimic Now().
    '
    ' 2016-06-09. Gustav Brock, Cactus Data ApS, CPH.
    '
    Public Function UtcNow() As Date
    
        Dim SysTime     As SystemTime
        Dim Datetime    As Date
    
        ' Retrieve current UTC date/time.
        GetSystemTime SysTime
    
        Datetime = _
            DateSerial(SysTime.wYear, SysTime.wMonth, SysTime.wDay) + _
            TimeSerial(SysTime.wHour, SysTime.wMinute, SysTime.wSecond)
    
        UtcNow = Datetime
    
    End Function
    

    Then use DateDiff("n", UtcNow, Now) to find the difference between UTC time and local time, and then add this to the retrieved filetime.