Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue using scientific notation as a parameter for Get-Date

Tags:

powershell

Per this Code-Golf tip, in PowerShell you can use scientific notation to easily generate numbers which are powers of 10: https://codegolf.stackexchange.com/a/193/6776

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

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

Yet if I use the scientific notation, even with brackets (i.e. date (1e7)) I get an error:

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 Get-Date's default (date) parameter?

like image 516
JohnLBevan Avatar asked Dec 31 '16 13:12

JohnLBevan


1 Answers

This is because 1e7 gets outputed as a double, so you just have to cast it to an integer:

date ([int]1e7)

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

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

IsPublic IsSerial Name   BaseType        
-------- -------- ----   --------        
True     True     Double System.ValueType

Edit: Shortest script probably is:

1e7l|date

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

like image 85
Martin Brandl Avatar answered Oct 23 '22 01:10

Martin Brandl