简体   繁体   English

Powershell - 将当地时间转换为不同的时区

[英]Powershell - convert local time to different timezone

I am trying to convert my system time (EST) to a different timezone (GMT Standard Time) using PowerShell. 我正在尝试使用PowerShell将我的系统时间(EST)转换为不同的时区(GMT标准时间)。 I have to run this PowerShell command through my RPA automation software so I am looking to use a single command (instead of PS script) to accomplish this task if possible. 我必须通过我的RPA自动化软件运行此PowerShell命令,因此我希望使用单个命令(而不是PS脚本)来完成此任务。 Here's the command I am trying to use: 这是我试图使用的命令:

$test = Get-Date
[System.TimeZoneInfo}::ConvertTime($test, 'GMT Standard Time')

First line is just to show the logic I am thinking but I will pass it as a variable and so that I truly have to execute only one line of code. 第一行只是为了显示我正在思考的逻辑,但我将它作为一个变量传递给我,所以我真的必须只执行一行代码。 However, I get the following error: 但是,我收到以下错误:

PS C:\Users\samsi> [System.TimeZoneInfo]::ConvertTime($test, 'GMT Standard Time')
Cannot find an overload for "ConvertTime" and the argument count: "2".
At line:1 char:1
+ [System.TimeZoneInfo]::ConvertTime($test, 'GMT Standard Time')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

Clearly, I don't know much about PowerShell, can someone please help me with this command. 显然,我对PowerShell了解不多,有人可以帮我解决这个问题。

Try this and see if it gets you what you're after. 试试这个,看看它是否能让你得到你想要的东西。

[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($(Get-Date), [System.TimeZoneInfo]::Local.Id, 'GMT Standard Time')

or if you prefer storing the current date in a variable first (the way you have it in your code) 或者如果您希望首先将当前日期存储在变量中(您在代码中的方式)

$test = Get-Date
[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($test, [System.TimeZoneInfo]::Local.Id, 'GMT Standard Time')

Let's look at how to diagnose this. 我们来看看如何诊断它。

The error message: 错误消息:

Cannot find an overload for "ConvertTime" and the argument count: "2". 找不到“ConvertTime”和参数计数的重载:“2”。

is a common one on PowerShell when calling methods on objects. 在对象上调用方法时,它是PowerShell上常见的一个。 Sometimes it's misleading. 有时它会产生误导。 It always means that given the arguments you supplied, none of the method's overloads matched. 它总是意味着给定您提供的参数,方法的重载都没有匹配。

Aside: each method has one or more ways you can call it, with parameters of different types, or different number or order of parameters. 除此之外:每种方法都有一种或多种方法可以调用它,具有不同类型的参数,或不同数量或参数顺序。

In PowerShell, you can see all the overloads by "invoking" the method without parentheses or arguments. 在PowerShell中,您可以通过“调用”没有括号或参数的方法来查看所有重载。 So: 所以:

[System.TimeZoneInfo]::ConvertTime

Result: 结果:

 OverloadDefinitions ------------------- static System.DateTimeOffset ConvertTime(System.DateTimeOffset dateTimeOffset, System.TimeZoneInfo destinationTimeZone) static datetime ConvertTime(datetime dateTime, System.TimeZoneInfo destinationTimeZone) static datetime ConvertTime(datetime dateTime, System.TimeZoneInfo sourceTimeZone, System.TimeZoneInfo destinationTimeZone) 

The error message in PowerShell always references the number of arguments, but it sometimes also means that you didn't use the correct type . PowerShell中的错误消息始终引用参数的数量 ,但有时也意味着您没有使用正确的类型

So in your example, you did supply 2 arguments, which makes the error confusing. 因此,在您的示例中,您确实提供了2个参数,这使得错误令人困惑。

But the second argument you supplied is a [string] . 但是你提供的第二个参数是[string] Looking at the available overloads, you can see that none of the second arguments take a string, they are all looking for an object of type [System.TimeZoneInfo] . 查看可用的重载,您可以看到没有第二个参数采用字符串,它们都在寻找[System.TimeZoneInfo]类型的对象。

Sometimes, you can use a different type, if there's an implicit cast available. 有时,如果有可用的隐式转换,您可以使用其他类型。 For example if a method takes a parameter of type [System.Net.IPAddress] then you can give a string like '127.0.0.1' . 例如,如果方法采用[System.Net.IPAddress]类型的参数,那么您可以给出类似'127.0.0.1'的字符串。 That's because [System.Net.IPAddress] knows how to convert an IP string into the object. 那是因为[System.Net.IPAddress]知道如何将IP字符串转换为对象。 You can see this by doing something like '127.0.0.1' -as [System.Net.IPAddress] or [System.Net.IPAddress]'127.0.0.1' . 您可以通过执行类似'127.0.0.1' -as [System.Net.IPAddress][System.Net.IPAddress]'127.0.0.1'来查看此[System.Net.IPAddress]'127.0.0.1'

Going back to your use case: it seems you either cannot cast a string to the TimeZoneInfo type, or your string is not valid for that purpose (that is, the cast failed). 回到你的用例:你似乎要么不能将字符串TimeZoneInfo转换为TimeZoneInfo类型,要么你的字符串无法用于那个目的(也就是说,强制转换失败)。

So you should first figure out how to create or retrieve a TimeZoneInfo object that represents what you want. 因此,您应首先弄清楚如何创建或检索表示您想要的TimeZoneInfo对象。

It looks to me like [System.TimeZoneInfo]::GetSystemTimeZones() returns an array of all the time zones on your system. 它看起来像[System.TimeZoneInfo]::GetSystemTimeZones()返回系统上所有时区的数组。

Filtering that list to find the one you want seems like a good idea. 过滤该列表以找到您想要的列表似乎是一个好主意。 Looking at the list, I can see the string you want to use is in the StandardName property so I'll use this to get the right one: 查看列表,我可以看到您要使用的字符串位于StandardName属性中,因此我将使用它来获取正确的字符串:

$gmt = [System.TimeZoneInfo]::GetSystemTimeZones().Where({$_.StandardName -eq 'GMT Standard Time'})[0] # it's an array, so get the first one

Then you can call your original method with that object: 然后,您可以使用该对象调用原始方法:

[System.TimeZoneInfo]::ConvertTime($test, $gmt)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM