So Windows has lots of date formats to choose from. These are a few and functions to convert between them and Datetime.

Datetime

The default timeformat that we are using in .NET and Powershell. This is probably the first date function you will learn to use. Or you can call on the .NET class.

Get-Date
[datetime]::get_Now()

FileTime

Microsoft built time format that calculates number of 100 ns intervals since January 1, 1601. Yes this is a really large number. But even though the name suggest this is always for files it isn’t. Also in some file systems the resolution isn’t 100 ns just because the format has that as the smallest incrment.

I did a blog entry about a small discrepancy depending when creating dates using different methods and how they differed by 100ns. This was most easy to spot while looking at the time in a FileTime format.

PS C:\Users\virot> (Get-Date).ToFileTime()
131244986538958951
PS C:\Users\virot> [datetime]::fromfiletime((Get-Date).ToFileTime())
den 24 november 2016 23:04:25
PS C:\Users\virot> [datetime]::fromfiletime(131244986538958951)
den 24 november 2016 23:04:13

DMTF (Distributed Management Task Force) DateTime

As used by AD for some attributes and WMI. The format is almost easy to read.

PS C:\Users\virot> [Management.ManagementDateTimeConverter]::ToDmtfDateTime((get-date))
20161124225058.082190+060
PS C:\Users\virot> [Management.ManagementDateTimeConverter]::ToDateTime('20161124225058.082190+060')
den 24 november 2016 22:50:58

Unix Epoch

This is the standard format used by *nix based systems. Number of seconds since January 1, 1970.

$unixEpochStart = new-object DateTime 1970,1,1,0,0,0,([DateTimeKind]::Utc)
[int]([DateTime]::UtcNow - $unixEpochStart).TotalSeconds
[int]([DateTime]::UtcNow - (new-object DateTime 1970, 1, 1, 0, 0, 0,([DateTimeKind]::Utc))).TotalSeconds

Also if you dont care about being compatible to older version you could use the [DateTimeOffset] class.

[DateTimeOffset]::Now.ToUnixTimeSeconds()

What about the other way then? That is really simpler.

(new-object DateTime 1970,1,1,0,0,0,([DateTimeKind]::Utc)).AddSeconds(1481670828)

Sources