I want to import a CSV file with my working hours. Format is as follows:
Description;Start Time;End Time;Duration;Day
When I import the CSV, each object gets imported as string. I try to convert the colmn 'Duration' as [timespan] in order to sum this column. This does not work.
$import = Import-Csv -Path "C:\admin\test3\tst_tasks_Mai-2019.csv" -Delimiter ";" |
Sort-Object -Top 15
$sort_description = $import | Sort-Object {$_.Description}
$set_dateformat = $sort_description | ForEach-Object {
$_.Duration = [timespan]$_.Duration
}
Error Message:
Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.TimeSpan"
Times and dates have many different formats based on culture, with many possible places for ambiguity... one string doesn't always (or even often) map to exactly one one TimeSpan. Therefore, it's not enough to simply cast the string via
[timespan]. You must parse the string.You can try this, but I'm not sure you won't also need to do something else to create the data project you're after, or use a
Parse()overload that allows you to be more precise about the expected format: