11

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.

1
  • 1
    I know nothing about JSON which is why this is only a comment but I use string manipulation to get the output you were looking for (Get-Date | Select-Object -Property Date | ConvertTo-Json).Split('"') | Where-Object{$_ -match "\\"}. It's dirty but split into array on quotes and match the array item with a backslash (2 since it needs to be escaped in regex.) \/Date(1411704000000)\/ Commented Sep 26, 2014 at 20:52

5 Answers 5

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I knew it was always going to be ugly but this one works. Luckily I don't have to worry about converting back.
8

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.

4 Comments

Thanks. This does work but leaves me with more work to do with casting and wrapping in a "Date()" string than the other answer.
Is this how you would convert it back? $utc = (get-date '1/1/1970') + [timespan]::frommilliseconds($jdate); $utc = [datetime]::SpecifyKind($utc,'utc'); $utc.ToLocalTime() Note that convertfrom-json returns utc times.
This is better option if you want to consume the json in java script. The output of this can go into new Date() of js.
The real problem is that there is no such thing as a JSON date. The JSON specification doesn't have a date type. Each implementation is welcome to use their own method. Microsoft chose this way, pretty much everyone else went with ISO-8601 formatted dates in strings.
0

I know this is old but Google led me here. I'm using Invoke-RestMethod to send/receive JSON data, including timestamps in the /Date(1411704000000)/ format. I managed to convert from PowerShell to JSON using the following:

[System.DateTime]$(Get-Date).DateTime

Example:

@{DateTime = [System.DateTime]$(Get-Date).DateTime} | ConvertTo-Json

Returns:

{
    "DateTime":  "\/Date(1484023637000)\/"
}

1 Comment

That json date format is problematic and even convertfrom-json doesn't convert it back properly. Powershell 6 converts a datetime to json differently.
0

Try use [System.Datetime]::Now instead Get-Date cmdlet

Comments

0

I found it, the solution is:

Get-Date | Select-Object -Property @{ Name = 'Date';  Expression = {$_.Date.ToString("dd-MM-yyyy hh:mm:ss")}} | ConvertTo-Json***

Output:

{
    "Date":  "16-02-2024 12:00:00"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.