使用科学记数法作为Get-Date参数的问题

Joh*_*van 6 powershell

根据此Code-Golf提示,在PowerShell中,您可以使用科学记数法轻松生成10的幂数:https://codegolf.stackexchange.com/a/193/6776

1e7产生数字10,000,000.

如果我将此值传递给get-date(或别名date,为了代码高尔夫的目的)我得到一秒钟:ie date 10000000=> 01 January 0001 00:00:01.

然而,如果我使用科学记数法,即使使用括号(即date (1e7)),我也会收到错误:

Get-Date : Cannot bind parameter 'Date'. Cannot convert value "10000000" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
At line:1 char:6
+ date (1e7)
+      ~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand
Run Code Online (Sandbox Code Playgroud)

有没有办法将科学记数法与Get-Date的默认(日期)参数一起使用?

Mar*_*ndl 6

这是因为1e7输出为double,所以你只需要将其转换为整数:

date ([int]1e7)
Run Code Online (Sandbox Code Playgroud)

如果GetType在输出上调用方法,则可以检查:

(1e7).GetType() | Format-Table -AutoSize

IsPublic IsSerial Name   BaseType        
-------- -------- ----   --------        
True     True     Double System.ValueType
Run Code Online (Sandbox Code Playgroud)

编辑: 最短的脚本可能是:

1e7l|date
Run Code Online (Sandbox Code Playgroud)

这取自PetSerAls注释 - 只是使用管道而不是括号删除了另一个字符.