Forum Discussion

Anonymous's avatar
Anonymous
Jul 31, 2019

Powershell Date Formatting

Hi Guys,

 

I have a column 'CreationDate' in a csv. The values in this column are in the below format.

 

7/30/2019 2:32:57 PM

7/30/2019 11:31:05 AM 

 

I want to read these values from the csv and convert them into the below format respectively which is a [datetime] format.

 

7/30/2019 14:32

7/30/2019 11:31

 

Can someone help me with the windows powershell syntax of this please ?

1 Reply

  • gastone's avatar
    gastone
    Brass Contributor

    Deleted

    Assuming a csv file like this test.csv :

    file,CreationDate
    Broccoli,7/30/2019 2:32:57 PM
    Carrots,7/30/2019 11:31:05 AM
    Beans,1/30/2019 1:31:05 AM

    the powershell code will be

     

     

     

     

    Import-Csv .\test.csv|ForEach-Object{$($_.file)+','+([datetime]"$($_.creationdate)").tostring('G')}>>newtest.csv

     

     

     

     

    the changing date format in powershell is

    ([datetime]'7/30/2019 2:32:57 PM').tostring('G')

     

    https://devblogs.microsoft.com/scripting/formatting-date-strings-with-powershell/

    G

    General date pattern with long time pattern

     2009-06-15T13:45:30 -> 6/15/2009 1:45:30 PM en-US

    2009-06-15T13:45:30 -> 15/06/2009 13:45:30 es-ES

    2009-06-15T13:45:30 -> 2009/6/15 13:45:30 zh-CN

    Bye Gas

Resources