# JSON date format problem

**URL:** https://forums.powershell.org/t/json-date-format-problem/5947
**Category:** PowerShell Help
**Created:** [March 8, 2016, 12:14am UTC](https://forums.powershell.org/t/json-date-format-problem/5947 "2016-03-08T00:14:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![stig-sornsen](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@stig-sornsen](https://forums.powershell.org/u/stig-sornsen)
#### Post date: [March 8, 2016, 12:14am UTC](https://forums.powershell.org/t/json-date-format-problem/5947/1 "2016-03-08T00:14:06Z")

</div>

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:

`
$object = New-Object psobject -Property @{ DateTime = (Get-Date) }
$object | ConvertTo-Json | ConvertFrom-Json | gm
`

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**

But why oh why?

Any thoughts?

Best regards  
Stig

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [March 8, 2016, 12:40am UTC](https://forums.powershell.org/t/json-date-format-problem/5947/2 "2016-03-08T00:40:00Z")

</div>

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).

You can report the bug over on UserVoice: [https://windowsserver.uservoice.com/forums/301869-powershell/category/148044-powershell-engine](https://windowsserver.uservoice.com/forums/301869-powershell/category/148044-powershell-engine)

---

<div class="post-metadata">

### Author: ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)
#### Post date: [March 8, 2016, 1:28am UTC](https://forums.powershell.org/t/json-date-format-problem/5947/3 "2016-03-08T01:28:26Z")

</div>

I guess I don’t understand the value added by creating a new object of an object. Does this get you what you were expecting?

```
$object = Get-Date
```

---

<div class="post-metadata">

### Author: ![stig-sornsen](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@stig-sornsen](https://forums.powershell.org/u/stig-sornsen)
#### Post date: [March 8, 2016, 5:28am UTC](https://forums.powershell.org/t/json-date-format-problem/5947/4 "2016-03-08T05:28:24Z")

</div>

Bob, no sorry.  
I was just trying to simulate getting back a class with a DateTime property - just like from my Web API.

Maybe this is more a C# question?

`
  public HttpResponseMessage Testing()
        {
            Test test = new Test();
            test.TestDate = DateTime.Now;
            return Request.CreateResponse(HttpStatusCode.OK, test);
        }
```
public class Test
{
    public DateTime TestDate { get; set; }
}

```
`

In PS:  
`
Invoke-RestMethod $url -Method Get | gm
`

What I get return:  
``

`
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition

`

`Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
TestDate NoteProperty string TestDate=2016-03-08T19:52:45.2883004+01:00
`

I would have thought that TestDate would be a DateTime and not string…

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:42pm UTC](https://forums.powershell.org/t/json-date-format-problem/5947/5 "2024-05-16T20:42:41Z")

</div>


