英文:
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
- $startTime = [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
-
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : TimeZoneNotFoundException**
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.
英文:
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
- $startTime = [TimeZoneInfo]::ConvertTimeBySystemTimeZoneId(
-
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : TimeZoneNotFoundException**
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.
答案1
得分: 1
代码部分不要翻译:
"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
."
"When you're not sure the exact name for destinationTimeZoneId
you're looking for you can use:
[TimeZoneInfo]::GetSystemTimeZones() | Select-Object Id, DisplayName
英文:
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
答案2
得分: 0
在PowerShell中,我首先定义变量,然后喜欢从那里开始工作。
我会这样做:
$localDate = Get-Date
$targetTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central European Standard Time")
$convertedTime = [System.TimeZoneInfo]::ConvertTime($localDate, $targetTimeZone)
$formattedTime = $convertedTime.ToString("yyyy-MM-ddTHH:mm:ss")
Write-Host $formattedTime
英文:
In Powershell, the first thing I do is define variables and then I like to work from there.
I would do it like that:
$localDate = Get-Date
$targetTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central European Standard Time")
$convertedTime = [System.TimeZoneInfo]::ConvertTime($localDate, $targetTimeZone)
$formattedTime = $convertedTime.ToString("yyyy-MM-ddTHH:mm:ss")
Write-Host $formattedTime
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论