Change the date format from Epoch to datetime in PowerShell

0

I have a String that represents a time in Epoch and I need the correct format to be able to enter it into a SQL Server database, from PowerShell. So far I can change the Epoch format to a very long date format, EXAMPLE:

Date in Epoch to convert: 746181000

Function I use:

[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds('746181000')) 

The output is as follows:

Tuesday, August 24, 1993 3:30:00 AM

What I really need is the date in this format: yyyy-MM-dd HH:MM:ss

So my desired output would be 1993-08-24 03:30:00

    
asked by Ricardo Soria 10.10.2018 в 19:23
source

1 answer

0

You can do the following:

Depending on the function you define earlier:

[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds('746181000'))

You can use the command Get-Date next to the Format parameter to manipulate the output according to your need, as follows:

$epochOut = [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds('746181000'))
$outputFixed = $epochOut | Get-Date -Format "yyyy-MM-dd hh:mm:ss"
    
answered by 11.10.2018 / 15:20
source