locked
Convert items LastWriteTime to JSON (Round-trip date/time pattern) RRS feed

  • Question

  • Hello,

    can you please help me convert LastWriteTime to "Round-trip date/time pattern" format?

    for example

    Get-Date -format o 

    will return like this 

    "2014-07-02T12:50:49.3494995+04:00"

     if I try convert with

    Get-date | Convertto-Json

    I`ll get like this:

    "\/Date(1411846057456)\/" 

    Elasticsearch does not recognize this format as "Date" tape  and perceives as a string tape.

    My question:

    How can I convert LastWriteTime "Friday, May 29, 2020 12:00:00 AM"  object attribute to this one "2020-05-12T12:00:00.3494995+04:00" ?

    Thank you.

    Friday, May 29, 2020 7:58 AM

Answers

  • Get-Date (Get-Item $env:APPDATA).LastWriteTime -Format o


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Friday, May 29, 2020 11:36 AM
  • [pscustomobject]@{Date=$d = Get-Date (Get-Item $env:APPDATA).LastWriteTime -Format o} | ConvertTo-Json


    \_(ツ)_/

    Friday, May 29, 2020 2:54 PM

All replies

  • My question:

    How can I convert LastWriteTime "Friday, May 29, 2020 12:00:00 AM"  object attribute to this one "2020-05-12T12:00:00.3494995+04:00" ?

    When the command you use in your first code snippet already provides the exact format you want why would you like to convert it?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Friday, May 29, 2020 8:05 AM
  • My question:

    How can I convert LastWriteTime "Friday, May 29, 2020 12:00:00 AM"  object attribute to this one "2020-05-12T12:00:00.3494995+04:00" ?

    When the command you use in your first code snippet already provides the exact format you want why would you like to convert it?

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    If I pass in this form "Friday, May 29, 2020 12:00:00 AM" - elastic takes this as a string type.
    If I pass the line in this form, "2020-05-12T12: 00: 00.3494995 + 04: 00" elastic takes this as a date type.

    So Get-Date I can convert by using -o.

    But when I make Gat-Item I will receive LastWriteTime date in format like "Friday, May 29, 2020 12:00:00 AM". I want convert it to format like this "2020-05-12T12: 00: 00.3494995 + 04: 00" and add this one to JSON object for push it to Elastic.


    In other words, how can I get format like this "2020-05-12T12: 00: 00.3494995 + 04: 00" when I do this (Get-Item $env:APPDATA).LastWriteTime.Datetime

    (Get-Item $env:APPDATA).LastWriteTime.Datetime
    Saturday, May 23, 2020 12:44:40 AM



    Friday, May 29, 2020 8:50 AM
  • Get-Date (Get-Item $env:APPDATA).LastWriteTime -Format o


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Friday, May 29, 2020 11:36 AM
  • [pscustomobject]@{Date=$d = Get-Date (Get-Item $env:APPDATA).LastWriteTime -Format o} | ConvertTo-Json


    \_(ツ)_/

    Friday, May 29, 2020 2:54 PM
  • Thank you, guys!
    Friday, May 29, 2020 5:24 PM