Get-Date cmdlet outputs wrong year from "FileTime" value.
See original GitHub issueSteps 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:
- Created 3 years ago
- Comments:12 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It is clear, as explained - and without an explicit (hypothetical) parameter such as
-FromFileTime
, what you’re asking for cannot be implemented (see below).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:
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.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. IfGet-Date
returns a datetime object, then why not give it the same capability asSystem.Datetime
class? For example: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.