0

Currently i am using the below function to convert the epoch time

Function convertFrom-epoch {
    [CmdletBinding()]
    param ([Parameter(ValueFromPipeline=$true)]$epochdate)
    
    if (!$psboundparameters.count) {help -ex convertFrom-epoch | Out-String | Remove-EmptyLines; return}
    if (("$epochdate").length -gt 10 ) {(Get-Date -Date "01/01/1970").AddMilliseconds($epochdate)}
    else {(Get-Date -Date "01/01/1970").AddSeconds($epochdate)}
}

convertFrom-epoch 1597044600

which is resulting me GMT time Monday, 10 August 2020 07:30:00

Is there is way to change the time to CDT time instead of GMT.

Empty Coder
  • 589
  • 6
  • 19

2 Answers2

1
Function convertFrom-epoch {
    [CmdletBinding()]
    param ([Parameter(ValueFromPipeline=$true)]$epochdate)
    
    if (!$psboundparameters.count) {help -ex convertFrom-epoch | Out-String | Remove-EmptyLines; return}
    if (("$epochdate").length -gt 10 ) {(Get-Date -Date "01/01/1970").AddMilliseconds($epochdate)}
    else { $Result = (Get-Date -Date "01/01/1970").AddSeconds($epochdate) }

    $cstzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")
    [System.TimeZoneInfo]::ConvertTimeFromUtc($Result.ToUniversalTime(), $cstzone)
}

convertFrom-epoch 1599838249

I took inspiration from this question: Create datetime object of other timezone in Powershell

codaamok
  • 717
  • 3
  • 11
  • 21
0
Function convertFrom-epoch {
    [CmdletBinding()]
    param ([Parameter(ValueFromPipeline=$true)]$epochdate)
    
    if (!$psboundparameters.count) {help -ex convertFrom-epoch | Out-String | Remove-EmptyLines; return}
    if (("$epochdate").length -gt 10 ) {(Get-Date -Date "01/01/1970").AddMilliseconds($epochdate)}
    else {[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date -Date "01/01/1970").AddSeconds($epochdate), 'America/Chicago')}
}

convertFrom-epoch 1597044600

The "Central Standard Time" does not work for me. My system has "America/Chicago":

[System.TimeZoneInfo]::GetSystemTimeZones() | % {if ($_.Id -eq "America/Chicago") {$_}}

Id                         : America/Chicago
DisplayName                : (UTC-06:00) Central Standard Time
StandardName               : Central Standard Time
DaylightName               : Central Daylight Time
BaseUtcOffset              : -06:00:00
SupportsDaylightSavingTime : True

So I assume the day time saving time will automatically adjusted, i.e. it will displayed the time at Chicago.

puravidaso
  • 1,013
  • 1
  • 5
  • 22