‘unable to find type [date]’ and other date-related powershell errors

This post just goes through a bunch of errors I got while trying to subtract one date from another. A lot of stuff on the internet quite rightly focuses on ‘how to’ do things in Powershell. I thought it might be useful to post up some examples of ‘how not to’.

I’ve got a tendency to just start trying things out rather than reading the help text. This isn’t typically a good idea, but it does have the advantage of meaning that you do find out what the error message are 🙂

Unable to find type [date]

Error: “String was not recognized as a valid DateTime.”

the right way – $(get-date) – $PaulsDateOfBirth

Exception setting “End”: “Cannot convert null to type “System.DateTime”.”

Error: “String was not recognized as a valid DateTime.”

the solution – use get-date

Unable to find type [date]

First:

PS> [date]$PaulsDateOfBirth = '18 June 1942'

returned this:

Unable to find type [date].
At line:1 char:1
+ [date]$PaulsDateOfBirth = '18 June 1942'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (date:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

This is fairly simple. The type I was thinking of is called =datetime=, not =date=, so the following ‘works’ in that it returns no errors

PS > [datetime]$PaulsDateOfBirth = '18 June 1942'

Error: “String was not recognized as a valid DateTime.”

So next I did this

get-date - $PaulsDateOfBirth

which is rubbish. I got this:

Get-Date : Cannot bind parameter 'Date'. Cannot convert value "-" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

At line:3 char:10

+ get-date - $PaulsDateOfBirth

+          ~
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand

the right way – $(get-date) – $PaulsDateOfBirth

This is much better:

$(get-date) - $PaulsDateOfBirth

which I think is actually returning the right result.

Days              : 26818
Hours             : 17
Minutes           : 53
Seconds           : 44
Milliseconds      : 346
Ticks             : 23171396243469845
TotalDays         : 26818.7456521642
TotalHours        : 643649.89565194
TotalMinutes      : 38618993.7391164
TotalSeconds      : 2317139624.34698
TotalMilliseconds : 2317139624346.98

Exception setting “End”: “Cannot convert null to type “System.DateTime”.”

Having got that far I wanted to turn the one-liner into a function that would return Paul’s age on any given day. I’ve just pasted in the lines which are relevant to the error here:

$DateOfAllbumRelease = $(get-date))
$PaulsDateOfBirth = [datetime]"1942-06-18"
$PaulsAge = new-timespan $PaulsDateOfBirth $DateOFAlbumRelease
$PaulsAge

The code looks reasonable…but I got this

get-PaulsAge
New-TimeSpan : Cannot bind parameter 'End' to the target. Exception setting "End": "Cannot convert null to type "System.DateTime"."
At line:6 char:44
+ $PaulsAge = new-timespan $PaulsDateOfBirth $DateOFAlbumRelease
+                                            ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewTimeSpanCommand

0

This is a simple one…especially so given the knowledge that I often mis-spell ‘Album’ as ‘Allbum’. Correcting the type, the function then worked:

get-PaulsAge
73.4739726027397

Error: “String was not recognized as a valid DateTime.”

What I actually wanted to do was call the function several times for different dates. I had this list:

22/03/63
26/04/63
22/11/63
19/06/64
10/07/64
4/12/64
6/08/65
3/12/65
5/08/66
10/12/66
1/06/67
22/11/68
17/01/69
26/09/69
8/05/70

…but you cant just put [datetime] in front of it

PS > [datetime]"22/03/63"
Cannot convert value "22/03/63" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
At line:1 char:1
+ [datetime]"22/03/63"
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocationWithFormatProvider


Incidentally it still doesn’t work if you try using the string as the value for a typed variable

[datetime]$X="22/03/63"
Cannot convert value "22/03/63" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
At line:1 char:1
+ [datetime]$X="22/03/63"
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

the solution – use get-date

However, get-date does happily convert the same string to a date, and return it as such to standard output

PS > get-date "22/03/63"
22 March 1963 00:00:00

…and

PS > get-date "1/03/63"
01 March 1963 00:00:00

So with my function defined as:

PS > Function get-PaulsAge {
param ($DateOfAlbumRelease = $(get-date))
$PaulsDateOfBirth = [datetime]"1942-06-18"
$PaulsAge = new-timespan $PaulsDateOfBirth $DateOFAlbumRelease
$PaulsAgeInDays = $PaulsAge.days 
$PaulsAgeInYears = $PaulsAge.days / 365
$PaulsAgeInYears
}

… I could do this

get-PaulsAge $(get-date "22/03/63")
get-PaulsAge $(get-date "26/04/63")
get-PaulsAge $(get-date "22/11/63")
get-PaulsAge $(get-date "19/06/64")
get-PaulsAge $(get-date "10/07/64")
get-PaulsAge $(get-date "4/12/64")
get-PaulsAge $(get-date "6/08/65")
get-PaulsAge $(get-date "3/12/65")
get-PaulsAge $(get-date "5/08/66")
get-PaulsAge $(get-date "10/12/66")
get-PaulsAge $(get-date "1/06/67")
get-PaulsAge $(get-date "22/11/68")
get-PaulsAge $(get-date "17/01/69")
get-PaulsAge $(get-date "26/09/69")
get-PaulsAge $(get-date "8/05/70")

and get the following, which as any fule kno, is Paul McCartney’s age, in years, when each of the Beatles’ LPs came out

20.772602739726
20.8684931506849
21.4438356164384
22.0191780821918
22.0767123287671
22.4794520547945
23.1506849315068
23.4767123287671
24.1479452054795
24.4958904109589
24.9698630136986
26.4493150684931
26.6027397260274
27.2931506849315
27.9068493150685
Advertisements