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

标签 powershell

根据此 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 - 刚刚使用管道而不是括号删除了另一个字符。

关于powershell - 使用科学记数法作为 Get-Date 参数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41408902/

相关文章:

powershell - 在PowerShell中转换日期时间格式

PowerShell 列出具有超过 1 人完全访问委托(delegate)权限的 Exchange 邮箱

powershell - 未引用的关键规则和最佳实践

Powershell - 获取计算机的上次注销日期和时间

powershell - 使用 Powershell 作业、运行空间或工作流时,线程是否在不同的内核上执行?

powershell - 自定义属性为空的 Get-ADUser

powershell v2 函数,如何在不命名第二个参数的情况下为第一个参数设置默认值?

linux - 尝试使用 PowerShell 从我的本地计算机复制文本文件内容并将其添加到 Linux 服务器中的另一个文件

powershell - 使用两个等长数组执行数学运算

powershell - 尝试在CSV列表中管道导入CSV