Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a datetime in PowerShell to JSON as \/Date(1411704000000)\/

I want to get the current date as a string in the following format:

\/Date(1411762618805)\/

I have been fighting with PowerShell and have tried the following, but it keeps wrapping the object with unwanted properties. I just need the value

Get-Date | ConvertTo-Json
# returns
{
  "value":  "\/Date(1411762618805)\/",
  "DisplayHint":  2,
  "DateTime":  "Friday, September 26, 2014 4:16:58 PM"
}

Of course if you try to convert back to an object with ConvertFrom-Json you are back with a .NET Date object.

I have gotten closer with

Get-Date | Select-Object -Property Date | ConvertTo-Json
{
  "Date":  "\/Date(1411704000000)\/"
}

But it is still wrapped in a Date child property. At the end of the day all I want is a string with Microsoft's ugly JSON format.

I only want to use the built in .NET JSON serializers if possible.

like image 857
kampsj Avatar asked Sep 26 '14 20:09

kampsj


People also ask

How do you format a date in JSON?

There is no date format in JSON, there's only strings a de-/serializer decides to map to date values. However, JavaScript built-in JSON object and ISO8601 contains all the information to be understand by human and computer and does not relies on the beginning of the computer era (1970-1-1).

How should a date value be stored in JSON?

JSON does not have a built-in type for date/time values. The general consensus is to store the date/time value as a string in ISO 8601 format.


2 Answers

There are two problem properties here, DateTime and DisplayHint and both require a different solution. The DateTime property is a ScriptProperty attached by the extended type system, so it exists on all DateTime objects automatically. You can remove it from all DateTime objects in the current session via Remove-TypeData

Get-TypeData System.DateTime | Remove-TypeData

The DisplayHint property is a NoteProperty that is added by the Get-Date cmdlet. I know of no way to suppress this from serialization. You can remove it explicitly from the DateTime:

Get-Date | foreach { $_.PSObject.Properties.Remove("DisplayHint"); $_ } | ConvertTo-Json

which will output

"\/Date(1411846057456)\/"

This is current date and time, if you only want the date you can do this

Get-Date | select -ExpandProperty Date | ConvertTo-Json

Here we don't have to remove the DisplayHint because it was not attached by Get-Date.

Don't even get me started on the hoops you will have to jump through to convert back to the proper date time object.

like image 102
Mike Zboray Avatar answered Oct 19 '22 12:10

Mike Zboray


So, the wonderful thing about JSON dates is... nothing. They are evil, and deserve to be punished.

So, what is a JSON date? It is the number of milliseconds since Unix Epoc (Jan 1, 1970), well, UTC time that is. So, that's great and all, but how do we get that without using the ConvertTo-JSON cmdlet? Easiest way I know of is this:

[int64]([datetime]::UtcNow)-(get-date "1/1/1970").TotalMilliseconds

That will get you the current date and time formatted for JSON. If you want a specific date or date/time you could do it, but you have to adjust for time zone, which we can do, it just gets long:

$target = "2/2/2014 6:32 PM"
[int64]((get-date $target).addhours((([datetime]::UtcNow)-(get-date)).Hours)-(get-date "1/1/1970")).totalmilliseconds
1391391120000

That would be the kick-off time for the last Super Bowl.

like image 34
TheMadTechnician Avatar answered Oct 19 '22 14:10

TheMadTechnician