Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

PowerShell Import-CSV format datetime: Data is be represented as 00:00:0 in the file output when the file has 2021-01-01 00:00:00.000

I have tried formatting the dates and have not had any luck.
Sample data:

2021-01-01 00:00:00.000,2021-01-24 00:00:00.000,2021-01-01 00:00:00.000,15,17,15,17,22,402219.89,402219.89,457621.22,457621.22,597385.79,10,1855180.96,169651.66,1235781.38,6.30,155963.88,155963.88,188556.81,188556.81,-214.63,-155.97
$data = import-csv $impcsv -header 'StartDate', 'EndDate', 'MonthStart','ytdDays', 'pyDays', 
 'MTDDays','PMTDDays','PFullMTDDays','ytd', 'mtd', 'pytd', 'pmtd', 'pfullmtd', 'whs', 'inventory', 
 'onorderinventory', 'onpo', 'yearpct', 'ytd_gp', 'mtd_gp', 'pytd_gp', 'pmtdd_gp', 'daily', 
 'daily_gp'  


 $count = 1 

 foreach($i in $data){ 

 [string]$StartDate = $I.StartDate 
 [string]$EndDate = $I.EndDate 
 [string]$MonthStart = $i.MonthStart
 [single]$ytdDays = $i.ytdDays
 [single]$pyDays = $i.pyDays
 [single]$MTDDays = $i.MTDDays
 [single]$PMTDDays = $i.PMTDDays
 [single]$PFullMTDDays = $i.PFullMTDDays
 [single]$ytd = $i.ytd
 [single]$MTD = $i.MTD
 [single]$pytd = $i.pytd 
 [single]$pmtd = $i.pytd 
 [single]$pfullmtd = $i.pfullmtd 
 [string]$whs = $i.whs 
 [single]$inventory = $i.inventory 
 [single]$onorderinventory = $i.onorderinventory 
 [single]$onpo = $i.onpo 
 [single]$yearpct = $i.yearpct 
 [single]$ytd_gp = $i.ytd_gp 
 [single]$mtd_gp = $i.mtd_gp 
 [single]$pytd_gp = $i.pytd_gp 
 [single]$pmtdd_gp = $i.pmtdd_gp 
 [single]$daily= $i.daily 
 [single]$daily_gp = $i.daily_gp 


  $query = "INSERT INTO $table (StartDate, EndDate, MonthStart, ytdDays, pyDays, 
                          MTDDays,PMTDDays,PFullMTDDays, ytd, mtd, pytd, pmtd, pfullmtd, whs, 
                          inventory,onorderinventory, onpo, yearpct, ytd_gp, mtd_gp, pytd_gp, 
                          pmtdd_gp, daily, daily_gp) 
         VALUES ('$StartDate', '$EndDate', '$MonthStart', '$ytdDays', '$pyDays', 
                 '$MTDDays','$PMTDDays','$PFullMTDDays','$ytd', '$mtd', '$pytd', '$pmtd', 
                 '$pfullmtd', '$whs', '$inventory', '$onorderinventory', '$onpo', 
                 '$yearpct','$ytd_gp','$mtd_gp', '$pytd_gp','$pmtdd_gp', '$daily', '$daily_gp')" 

  invoke-sqlcmd -Database $databaseName -ServerInstance $ServerName -Username $username -Password 
  $password  -Query $query
                    
  write-host $startDate 
  write-host "Processing row ..........$count" -foregroundcolor green 

  $count  = $count + 1 

  } 
question from:https://stackoverflow.com/questions/65905897/powershell-import-csv-format-datetime-data-is-be-represented-as-00000-in-the

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...