question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get-Date cmdlet outputs wrong year from "FileTime" value.

See original GitHub issue

Steps to reproduce


    $filetime = (Get-Date).ToFileTime()
    'Get-Date: {0:dd.MM.yyyy}' -f (Get-Date $filetime)
    'DateTime: {0:dd.MM.yyyy}' -f [datetime]::FromFileTime($fileTime)

Output:

Get-Date: 18.03.0421
DateTime: 18.03.2021

Expected behavior

Outputting the correct date

Actual behavior

Incorrect year value

Environment data

$PSVersionTable                                         
                                                           
Name                           Value                       
----                           -----                       
PSVersion                      7.1.3                       
PSEdition                      Core                        
GitCommitId                    7.1.3                       
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT                     
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}       
PSRemotingProtocolVersion      2.3                         
SerializationVersion           1.1.0.1                     
WSManStackVersion              3.0                         

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mklement0commented, Mar 19, 2021

why Get-Date trying interpret these values is not.

It is clear, as explained - and without an explicit (hypothetical) parameter such as -FromFileTime, what you’re asking for cannot be implemented (see below).

why not give it the same capability as System.Datetime class?

Generally speaking, it is not - and should not - be PowerShell’s goal to replicate all .NET APIs. Instead, PowerShell’s focus is on what matters from a perspective of a shell and automation - though there is substantial overlap, which is what makes PowerShell unique among shells.

Using .NET APIs, the solution is:

[datetime]::FromFileTime($filetime)

This complements your use of a .NET API to obtain the $filetime value to begin with - the .ToFileTime() method.


If you feel strongly enough that this functionality should be surfaced via the Get-Date cmdlet as well, I suggest you create a new issue of type “Feature Request/Idea” and ask for:

  • -AsFileTime to convert (from [datetime]) TO a FILETIME value
  • -FromFileTime to convert FROM a FILETIME value (to a [datetime] instance)

Note: no need for _UTC, as FILETIME values are by definition UTC timestamps.

1reaction
237dmitrycommented, Mar 19, 2021

All is clear but why Get-Date trying interpret these values is not. New-Timespan or (Get-Date) - (Get-Date) returns a more obvious result. If Get-Date returns a datetime object, then why not give it the same capability as System.Datetime class? For example:

Get-Date -FromFiletime_Uts_ $filetime

This would be logical to get the FileTime value and return the DateTime object from a single cmdlet. Without resorting to .net methods explicitly. It’s more a matter of style, not convenience. These are small things, but every little thing is important. I had never even thought of putting FileTime in Get-Date before. That’s why I was surprised. Good Night, my filetime is already near the morning.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Year wrong using Powershell script to pull dates from AD
In ActiveDirectory Microsoft specifics attibuts that deal with time are stored as a Windows file time. It's a 64-bit value that represents ...
Read more >
Issue with Get-Date conversion of FileDate returning year ...
I'm having an issue with Get-Date (which I suspect is an issue with the ... ToFileTime()) The returned value is: Tuesday, February 23, ......
Read more >
Get-Date Not Formatting Correctly in Function - PowerShell
I have a custom Logging module I created with various functions. One of the functions (it should be called a mini-function, it onl....
Read more >
how to use a file to write output to within a loop
Hello,. I am trying to modify the following PowerShell code to write to a file within the foreach loop's if statement. So instead...
Read more >
Get-Date - How to get and use Dates in PowerShell
Learn how to get, format, compare and calculate dates with the Get-Date cmdlet in PowerShell. All important features explained.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found