Problem using scientific notation as a parameter for Get-Date

In this PowerShell Code-Golf tip, you can use scientific notation to easily generate numbers with a power of 10: https://codegolf.stackexchange.com/a/193/6776

i.e. 1e7 prints the number 10,000,000 .

If I pass this get-date value (or the date alias, for code golf purposes), I get one second: i.e. date 10000000 => 01 January 0001 00:00:01 .

But if I use scientific notation, even with parentheses (i.e. date (1e7) ), I get an error message:

 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 

Question

Is there a way to use scientific notation with the default parameter (date) of Get-Date?

+6
source share
1 answer

This is because 1e7 is output as double , so you just need to pass it to an integer:

 date ([int]1e7) 

You can check this if you call the GetType method on the output:

 (1e7).GetType() | Format-Table -AutoSize IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Double System.ValueType 

Edit: The shortest script is possible:

 1e7l|date 

This is taken from PetSerAls comment - just deleted another character using a pipe instead of brackets.

+6
source

Source: https://habr.com/ru/post/1013668/


All Articles