Sorting a string that has date/time

Welcome Forums General PowerShell Q&A Sorting a string that has date/time

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #61066

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 260
    Helping Hand
    Rank: Contributor
    $data | Select-Object -Property  @{Name='Date';Expression={Get-Date $_.DateAdded}} | Sort-Object -Property Date
  • #61092

    Participant
    Points: 0
    Rank: Member

    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!

The topic ‘Sorting a string that has date/time’ is closed to new replies.