Issue with Get-Date conversion of FileDate returning year 0421

Greg Corey 21 Reputation points
2021-02-23T17:43:54.137+00:00

I'm having an issue with Get-Date (which I suspect is an issue with the underlying .NET framework) which can best be illustrated with this command:

Get-Date ((get-date).ToFileTime())

The returned value is:

Tuesday, February 23, 0421 5:41:34 PM

Is this a known bug or a behavior change in the framework?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,431 Reputation points
    2021-02-24T00:02:56.703+00:00

    EDIT: Not a bug. My previous answer (below) is incorrect. A DateTime object holds "number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.)". A FileTime is the number of ticks from 12:00 midnight, January 1, 1601 A.D.

    Very interesting! It looks like the "(Get-Date).ToFileTime"), which should be returning the number of ticks since 1-Jan-1601 00:00:00Z, isn't doing that. It's off by 1601 years!

    [int64]$Ftime=(get-date).ToFileTime()
    [int64]$Ctime=(get-date).ticks
    [int64]$Diff = $Ctime - $Ftime
    Get-Date $Diff
    
    [int64]$NewFtime = $Ftime + $Diff
    Get-Date $NewFtime
    

    However, if you're interested in getting the date as a number of ticks you can simply use "(Get-Date).Ticks"

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 32,326 Reputation points Microsoft Vendor
    2021-02-24T09:31:38.85+00:00

    Hi,

    A FileTime represents the number of 100-nanosecond intervals(ticks) since January 1, 1601 (UTC). If you want to convert a FileTime to DateTime you can use the [DateTime]::FromFileTime() method

    [DateTime]::FromFileTime((Get-Date).ToFileTime())  
    

    You may refer to the link below
    File Times

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments