Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

casting column from csv as date powershell

I have a csv that is similar to this

"fundName","MMFcusip","ticker","AsOfDate","SumHoldingPercent"
"BlackRock OH Muni MMP/Instit","'091927236'","COIXX","2/29/2012 12:00:00 AM","100.00000000200"
"Western Asset Inst US Treas Res","'52470G841'","CIIXX","2/29/2012 12:00:00 AM","100.00000000200"

Using powershell, how can I cast the "asOfDate" as a date/time when using the import-csv cmdlet?

EDIT: this is the line of code that I'm currently working with

$measuredDate = $today.AddDays(-21)
$staleDates = Import-Csv d:\path\file.csv | Foreach-Object {$_.AsOfDate = Get-Date $_.AsOfDate} | Where-Object {asOfDate -lt $measuredDate} | Measure-Object
like image 888
mhopkins321 Avatar asked May 14 '12 14:05

mhopkins321


1 Answers

Import-Csv file.csv |
Where-Object { ![string]::IsNullOrWhiteSpace($_.AsOfDate) } | 
Foreach-Object {
   $_.AsOfDate = $_.AsOfDate -as [datetime]
   $_
}
like image 189
Shay Levy Avatar answered Nov 12 '22 22:11

Shay Levy