pizzim13
pizzim13

Reputation: 1482

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

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

Upvotes: 7

Views: 35620

Answers (4)

Garric
Garric

Reputation: 724

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

Upvotes: 3

Manikandan Boopathi
Manikandan Boopathi

Reputation: 29

It is based on your local "long date" format.

enter image description here

Either you have to change your system "Long Date"

(OR) Use below command

(get-date).tostring("dd/MM/yyyy")

enter image description here

https://technet.microsoft.com/en-us/library/ee692801.aspx

Regards, Manikandan Boopathy

Upvotes: -1

Poul Jorgensen
Poul Jorgensen

Reputation: 41

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

Upvotes: 4

Massif
Massif

Reputation: 4433

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.)

Upvotes: 6

Related Questions