Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Edit csv column

I have a csv with 3 columns.

Date    Time    Event
12/19/2021  3:00pm  Low
12/19/2021  1:30pm  Low
12/20/2021  3:00pm  Low

I’m trying to subtract 5 hours from each row.
How can I keep each object in a datetime format?

$time0 = Import-Csv $Tempdest | Select 'Time'

$timeEst = $time0 | ForEach-Object {
Write-Host $_.
            ([datetime]$_).AddHours(-5)
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

By looking at the provided screenshot this should work, it would be updating the imported CSV in memory. If it doesn’t work it’s best if you can share with us the CSV as plain text.

$csv = Import-Csv path/to/csv.csv

$csv | ForEach-Object {
    $date = '{0} {1}' -f $_.Date, $_.Time -as [datetime]
    $date = $date.AddHours(-5)
    $_.Date = $date.ToShortDateString()
    $_.Time = $date.ToShortTimeString()
}

$csv | Format-Table
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading