← All updates

Engineering / Field note

PowerShell can turn JSON timestamp text into a date

Five tested examples show how PowerShell parses JSON timestamps, how -DateKind changes the result, and how to keep an application's text and time contracts explicit.

By Zyel · Published · 4 min read

A timestamp can arrive in JSON as text and emerge from PowerShell as a DateTime object. A later string-only check can reject that value even though the incoming document contained a quoted string. Before changing the check, decide whether the boundary promises timestamp text, a parsed instant, or both. The parser may have brought its own calendar.

This is established behavior reviewed on December 2, 2026. Microsoft documents -DateKind as introduced in PowerShell 7.5; version 7.5.0 was released on January 23, 2025. The examples below ran in PowerShell 7.6.9. They are original fictional inputs, with no private system or incident represented. [2] [7]

Observe the value after conversion

RFC 8259, published in December 2017, defines JSON strings, numbers, booleans, null, objects and arrays. It has no separate date value type. A quoted timestamp is therefore a JSON string; a consumer's choice to recognize it as a date belongs to that consumer's conversion behavior. [1]

PowerShell's versioned 7.5.0 implementation makes the choice explicit. Its ConvertFromJson path configures date parsing and timezone handling according to JsonDateKind. The default branch selects DateTime parsing; the String branch disables date parsing. Those settings explain why the resulting .NET type deserves inspection. [3]

Example 1 — Observe a parsed timestamp's runtime type · PowerShell
$json = '{"recordedAt":"2026-12-01T12:34:56Z"}'
$item = $json | ConvertFrom-Json
$value = $item.recordedAt
if ($value -isnot [datetime] -or $value.Kind -ne 'Utc') {
    throw 'Expected a parsed UTC DateTime in this example'
}
[pscustomobject]@{
    RuntimeType = $value.GetType().FullName
    IsText = $value -is [string]
    Kind = $value.Kind
}

In this fixture the value is System.DateTime, its Kind is Utc, and IsText is false. Microsoft defines -is and -isnot as tests of an object's .NET type. A string test at this point examines the converted object, rather than the quoted value that appeared in the original document. [4]

Preserve text when text is the contract

Microsoft documents the String date kind as preserving the timestamp value as a string for custom parsing. The next example checks that the option exists, keeps a fictional value with fractional seconds and an explicit offset, and asserts both its type and its exact decoded text. [2]

Example 2 — Preserve the decoded timestamp string · PowerShell
$command = Get-Command ConvertFrom-Json
if (-not $command.Parameters.ContainsKey('DateKind')) {
    throw 'This example requires PowerShell 7.5 or later'
}
$json = '{"recordedAt":"2026-12-01T12:34:56.1234000+02:00"}'
$item = $json | ConvertFrom-Json -DateKind String
$expected = '2026-12-01T12:34:56.1234000+02:00'
if ($item.recordedAt -isnot [string]) {
    throw 'Expected timestamp text'
}
if ($item.recordedAt -cne $expected) {
    throw 'Timestamp value changed'
}

This preserves the JSON string's decoded value. It does not promise byte-for-byte retention of the complete JSON document. For example, an escaped character can decode to the same character written directly. Keep the original input separately if the surrounding whitespace or escape spelling is itself part of the contract.

The runtime guard makes the requirement visible. Scripts using these DateKind options need PowerShell 7.5 or later. The versioned 7.4 command reference lacks the parameter; these examples do not install another runtime or offer an untested compatibility fallback. [8]

Choose the time representation deliberately

The documented Offset mode produces DateTimeOffset while retaining the input's numeric offset. Utc produces a DateTime in UTC. The following example asks for each representation and checks that they identify the same instant, while separately checking the retained offset and UTC Kind. [2]

Example 3 — Compare offset and UTC representations · PowerShell
$json = '{"recordedAt":"2026-12-01T12:34:56+02:00"}'
$withOffset = $json | ConvertFrom-Json -DateKind Offset
$asUtc = $json | ConvertFrom-Json -DateKind Utc
if ($withOffset.recordedAt -isnot [datetimeoffset]) {
    throw 'Expected DateTimeOffset'
}
if ($withOffset.recordedAt.Offset -ne [timespan]::FromHours(2)) {
    throw 'Original offset was not retained'
}
if ($asUtc.recordedAt.Kind -ne 'Utc') {
    throw 'Expected a UTC DateTime'
}
if ($withOffset.recordedAt.UtcDateTime -ne $asUtc.recordedAt) {
    throw 'The values should identify the same instant'
}

Equal instants need not have identical display text. Our sample's 12:34:56 at +02:00 corresponds to 10:34:56 UTC. A requirement to preserve the supplied text and a requirement to compare instants are different requirements. Make the intended one visible in names, assertions and serialized fields.

Validate the application's actual format

For a fictional export, suppose recordedAt must contain whole-second UTC text ending in uppercase Z. That is an application rule, narrower than the range of timestamp formats a general parser might accept. The next helper enforces that shape before checking whether the calendar date and time are valid.

.NET's DateTimeOffset.TryParseExact accepts an explicit format, culture provider and parsing styles, returning whether conversion succeeded. Our helper chooses invariant culture and UTC handling. It does not infer a timezone from the machine's display settings or accept a malformed calendar date merely because the punctuation looks right. [5]

Example 4 — Enforce a fictional UTC text contract · PowerShell
function Convert-StrictUtcStamp {
    param([object] $Value)
    if ($Value -isnot [string]) {
        throw 'Timestamp must arrive as text'
    }
    $shape = '\A[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\z'
    if ($Value -cnotmatch $shape) {
        throw 'Expected a whole-second UTC timestamp ending in Z'
    }
    $format = "yyyy-MM-dd'T'HH:mm:ss'Z'"
    $styles = [Globalization.DateTimeStyles]::AssumeUniversal -bor
        [Globalization.DateTimeStyles]::AdjustToUniversal
    $parsed = [datetimeoffset]::MinValue
    $ok = [datetimeoffset]::TryParseExact(
        $Value, $format, [Globalization.CultureInfo]::InvariantCulture,
        $styles, [ref] $parsed
    )
    if (-not $ok) { throw 'Invalid calendar date or time' }
    return $parsed
}
$stamp = Convert-StrictUtcStamp '2026-12-01T12:34:56Z'

The object parameter is intentional: the helper first checks that its caller actually supplied a string. Passing an already parsed DateTime is rejected by this particular contract. Accepting a useful time object elsewhere can be reasonable, but changing accepted types should be an explicit API decision.

This helper rejects offsets, fractional seconds, lowercase z and second sixty. Those are limits of the fictional whole-second contract, not a definition of every valid internet timestamp. A real application that permits other forms needs its own documented acceptance rules and tests.

Round-trip the representation you promised

Microsoft documents ConvertTo-Json as converting a .NET object to a JSON-formatted string. The final example formats the accepted instant as canonical UTC text, puts that text in a structured object, serializes it, then reads it using String mode. It checks both the resulting text and the parsed instant. [6]

Example 5 — Serialize and verify the intended representation · PowerShell
# Load Convert-StrictUtcStamp from Example 4 first.
$stamp = Convert-StrictUtcStamp '2026-12-01T12:34:56Z'
$format = "yyyy-MM-dd'T'HH:mm:ss'Z'"
$canonical = $stamp.UtcDateTime.ToString(
    $format, [Globalization.CultureInfo]::InvariantCulture
)
$payload = [ordered]@{
    recordedAt = $canonical
    label = 'fictional observation'
} | ConvertTo-Json -Depth 4 -Compress
$again = $payload | ConvertFrom-Json -DateKind String
if ($again.recordedAt -isnot [string] -or
    $again.recordedAt -cne $canonical) {
    throw 'The expected timestamp text did not round-trip'
}
$parsedAgain = Convert-StrictUtcStamp $again.recordedAt
if ($parsedAgain -ne $stamp) {
    throw 'The expected instant changed'
}

All five examples executed successfully in PowerShell 7.6.9. Nineteen additional checks cover valid and invalid leap days, wrong value types, missing or different zone markers, fractional seconds, whitespace, default no-zone parsing, decoded escapes and invariant parsing under three cultures. The examples call no external service and use no production account.

Older PowerShell runtimes were not executed in this review. The historical source and versioned documentation establish context; the recorded run establishes these examples' observed behavior in 7.6.9. When reviewing a JSON boundary, retain the incoming value, the conversion mode and the post-conversion type as separate observations.

Sources + further reading

Source dates describe the original material. Publication above is the date this Zyel article went live.

  1. RFC 8259: The JSON Data Interchange FormatIETF / RFC Editor · Checked Dec 2, 2026
  2. ConvertFrom-Json: DateKind optionsMicrosoft Learn · Checked Dec 2, 2026
  3. PowerShell 7.5.0 JSON conversion implementationMicrosoft / PowerShell · Checked Dec 2, 2026
  4. PowerShell .NET type operatorsMicrosoft Learn · Checked Dec 2, 2026
  5. DateTimeOffset.TryParseExactMicrosoft Learn · Checked Dec 2, 2026
  6. ConvertTo-JsonMicrosoft Learn · Checked Dec 2, 2026
  7. PowerShell v7.5.0 releaseMicrosoft / PowerShell · Checked Dec 2, 2026
  8. PowerShell 7.4 ConvertFrom-Json referenceMicrosoft Learn · Checked Dec 2, 2026

Reader discussion

What would you ask next?

Questions, useful corrections and thoughtful disagreement are welcome. Comments are reviewed before they appear.

No reader comments yet.

10–2,000 characters. Plain text. Leave out passwords, private project data and personal contact details. A blank display name appears as Anonymous.

Complete the CAPTCHA before sending. Verification runs on your device.