I have a problem in my C# Web API, where an action is return an object with DateTime property. However, when receiving it and converting it to PS object with ConvertFrom-Json the DateTime property is converted to a string.
I then began testing a bit and found it curious how JSON serialize DateTime in Powershell:
This gives me: DateTime NoteProperty System.Management.Automation.PSCustomObject DateTime=@{value=08-03-2016 13:36:27; DisplayHint=2; DateTime=8. marts 2016 14:36:27}
Too much…
(Get-Date).Date
Gives me: DateTime NoteProperty datetime DateTime=07-03-2016 23:00:00
Hurray! datetime property, however this is just the Date…
I thought I would be genius just to do this then: (Get-Date).DateTime
But that gives me a string again… DateTime NoteProperty string DateTime=8. marts 2016 14:38:37
I could ofcourse do this then: Get-Date($object | ConvertTo-Json | ConvertFrom-Json).Datetime
I get what I want: TypeName: System.DateTime
I’d call this a bug in ConvertTo-Json. It should be treating DateTime as a primitive type and not appending any of the extra ETS properties. (The same can be said for any other primitive type that JSON supports, but as far as I know, none of the other ones have extra properties tacked on by PowerShell’s type system).