powershellunix-timestampepochdynatrace

How to convert current date time into time zone format using PowerShell?


I am trying to convert the current date time into the format "2023-05-25T09:51:55" using time zone Central European Time using PowerShell but getting below error.

**Exception calling "ConvertTimeBySystemTimeZoneId" with "2" argument(s): "The time zone ID 'Central European Time' was not found on the local computer." At line:2 char:1

I have tried below code

**`$localDate = Get-Date $startTime = [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId( [datetimeoffset] $localDate, 'Central European Time' )

$dtoEST.ToString('o') -replace '.\d+(?=-)-\d+:\d+'`**

Could anyone please let me know the good and correct way to do it.


Solution

  • The issue with your code is due to a typo in the destinationTimeZoneId argument, it should be Central European Standard Time instead of Central European Time:

    $localDate = Get-Date
    [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
        [datetimeoffset] $localDate, 'Central European Standard Time').
        ToString('yyyy-MM-ddTHH:mm:ss', [cultureinfo]::InvariantCulture)
    

    When you're not sure the exact name for destinationTimeZoneId you're looking for you can use:

    [TimeZoneInfo]::GetSystemTimeZones() | Select-Object Id, DisplayName