根据此 Code-Golf 提示,在 PowerShell 中,您可以使用科学记数法轻松生成 10 的幂数:https://codegolf.stackexchange.com/a/193/6776
即1e7
产生数字 10,000,000
.
如果我将此值传递给 get-date
(或别名 date
,用于代码高尔夫)我得到一秒钟:即 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
问题
有没有办法将科学记数法与 Get-Date 的默认(日期)参数一起使用?
请您参考如下方法:
这是因为 1e7
输出为 双 ,所以你只需要将它转换为一个整数:
date ([int]1e7)
如果您调用
GetType
,您可以检查一下。输出方法:
(1e7).GetType() | Format-Table -AutoSize
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Double System.ValueType
编辑:
最短的脚本可能是:
1e7l|date
这取自 PetSerAls comment - 刚刚使用管道而不是括号删除了另一个字符。