如何使用PowerShell将当前日期时间转换为时区格式?

huangapple go评论64阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月25日 22:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定