none
How to format datetime in powershell RRS feed

  • Question

  • I am trying to format the datetime for my folders once they are sorted into descending order . At the moment I have datetime format like 25-04-2014 and I would like to to get displayed 25th april 2014 I am using this code but dont know how to use the formatting

    $sortedContent = $content |  Sort-Object LastWriteTime -Descending

    • Edited by k786 Wednesday, July 9, 2014 3:31 PM
    Wednesday, July 9, 2014 3:21 PM

Answers

All replies

  • Get-ChildItem | Select-Object Name, @{N='LastWrite';E={$_.LastWriteTime.ToString('dddd  dd MMMM yyyy')}}

    ¯\_(ツ)_/¯

    Wednesday, July 9, 2014 3:31 PM
  • is there any way i can modify my script because my script is working perfect all i have to do is to format the datetime

    $sortedContent = $content |  Sort-Object LastWriteTime -Descending

    Wednesday, July 9, 2014 3:36 PM
  • Help pipeline

    help select-object -examples

    http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯

    Wednesday, July 9, 2014 3:44 PM
  • i am unable to get it right some how, i dont know why it is so difficult to have formated datetime on sorted output

    Wednesday, July 9, 2014 4:10 PM
  • Why do you need to format the DateTime objects?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 9, 2014 4:17 PM
    Moderator
  • i am unable to get it right some how, i dont know why it is so difficult to have formated datetime on sorted output

    What have you tried?  I showed you how to generate a format with the custom calculated properties.  I also gave you pointers to the help and to the learning pages so you could learn how to use PowerShell.

    You have not shown us what you have tried.

    The pipeline link shows you how to add the the output.  You can also use Format-Table to get formatted output.

    There are many ways to do this but youy need to at least learn the very basic things or you will not ve able to understand any answers.

    Here is how to add this:

    $sortedContent = $content |  
         Sort-Object LastWriteTime -Descending | 
         Select-Object Name, @{N='LastWrite';E={$_.LastWriteTime.ToString('dddd  dd MMMM yyyy')}}

    Of course all of this depends on what you have put into the variable "$content"


    ¯\_(ツ)_/¯

    Wednesday, July 9, 2014 4:34 PM
  • The string representations of the DateTime objects in the output are exactly that: String representations. The objects are still DateTime objects. Why do you want/need to output formatted strings instead?


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 9, 2014 5:00 PM
    Moderator
  • jrv's code works, although the four dddd's will make the output for the date Friday 25 April 2014.

    Remove those four d's and only keep the other two d's if you want 25 April 2014 instead and only use one d if you don't want a leading zero on single digit dates.

    Resource on DateTime with PowerShell


    Thursday, July 10, 2014 5:42 PM