Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with epoch time in PowerShell using UniversalTime

This is probably a super simple question, but I'm a bit frustrated now because I can't find exactly what I'm looking for on the internet.

I am trying to convert a PowerShell DateTime to Epoch time using universal time.

I can do either, but can't seem to find a working command to do both.

Here is what I know I can do:

PS C:\WINDOWS\system32> (Get-Date).ToUniversalTime()
Wednesday, November 15, 2017 5:07:35 PM

And:

PS C:\WINDOWS\system32> Get-Date -UFormat %s
1510747694.20287

But, how do I combine them so that I get seconds since epoch in UTC? Everything I try just gives me errors about unexpected token "UFormat" or String does not have method "ToUniversalTime."

Thanks!

like image 960
Appleoddity Avatar asked Jan 30 '23 05:01

Appleoddity


1 Answers

If you look at Get-Help Get-Date the first parameter is a -Date option. We can use that to our advantage by wrapping our Get-Date in another Get-Date like so:

$epochseconds = Get-Date (Get-Date).ToUniversalTime() -UFormat %s
return $epochseconds
like image 142
Bryce McDonald Avatar answered Feb 02 '23 08:02

Bryce McDonald