Sorting a string that has date/time

This topic contains 2 replies, has 2 voices, and was last updated by  Scott Windmiller 9 months, 2 weeks ago.

  • Author
    Posts
  • #61066

    Scott Windmiller
    Participant

    I have a bunch of Dates and times that I think are just strings.

    $data | Select DateAdded
    5/20/2015 8:27:34 AM
    7/13/2016 8:10:58 AM
    6/04/2012 12:10:58 PM

    Wanting to sort would give me:

    $data | Select DateAdded | Sort DateAdded
    5/20/2015 8:27:34 AM
    6/04/2012 12:10:58 PM
    7/13/2016 8:10:58 AM

    But what I want is:

    6/04/2012 12:10:58 PM
    5/20/2015 8:27:34 AM
    7/13/2016 8:10:58 AM

    How can I get it to sort using the Year Month Day Time?

    Thanks!
    Scott

  • #61071

    Olaf Soyk
    Participant
    $data | Select-Object -Property  @{Name='Date';Expression={Get-Date $_.DateAdded}} | Sort-Object -Property Date
  • #61092

    Scott Windmiller
    Participant

    Thanks, this worked great. I think my hangup was thinking the Get-Date was only to get the current date/time and not actually setting something as a date/time object.

    Thanks for the help!

You must be logged in to reply to this topic.