Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: get-date & [datetime]::FromFileTime returns different values

Tags:

powershell

Why does get-date & [datetime]::FromFileTime returns different values when converting FileTime? An example:

Get-Date 129442497539436142

returns Thursday, March 10, 0411 4:55:53 PM, but

[datetime]::FromFileTime("129442497539436142")

returns Thursday, March 10, 2011 11:55:53 AM

like image 824
pizzim13 Avatar asked Mar 14 '11 15:03

pizzim13


3 Answers

They produce the same result for me, presumably because I'm in GMT.

(FromFileTime parses the time as UTC, Get-Date appears to be using your local time.)

like image 70
Massif Avatar answered Sep 21 '22 21:09

Massif


FileTimes are so-called Ticks. 10 million pass every second. Filetime are 0 at midnight, January 1st 1601 (UTC).

Get-date also have ticks, but the base of ticks used by Get-Date is NOT 1601.
It is January 1st year 1.
You can basically identify the 2 different types of ticks by the first digit.

Filetime ticks starts with digit 1 in the rough range of -100 to + 200 years from now.

The ticks using base on January 1st year 0 starts with 6 in roughly the same time range...

In PowerShell you can get verify Jan. 1st year 1 is tick 0 by typing:

[datetime]'0001-01-01' | Select-Object -property Ticks
like image 36
Poul Jorgensen Avatar answered Sep 19 '22 21:09

Poul Jorgensen


Get-Date get ticks from 01/01/0001 00:00

[datetime]::FromFileTimeUTC($a) get ticks from 01/01/1601 00:00

You wrote:

returns Thursday, March 10, 0411 4:55:53 PM, but
returns Thursday, March 10, 2011 11:55:53 AM

The difference is 1600 years

This is an example:

$a = ([datetime]::Now).Ticks - ([DateTime]("01/01/0001 00:00")).Ticks
Get-Date $a # get ticks from 01/01/0001 00:00
$a = ([datetime]::Now).Ticks - ([datetime]("01/01/1601 00:00")).Ticks
[datetime]::FromFileTimeUTC($a) # get ticks from 01/01/1601 00:00
like image 34
Garric Avatar answered Sep 22 '22 21:09

Garric