[英]Date format when exported to CSV using Powershell
我有一個Powershell腳本,該腳本執行sql查詢,然后將信息導出到CSV文件。 但是,CSV文件將所有DateTimes條目重新格式化為mm/d/yyyy hh:mm:ss
。 我需要輸入的格式為yyyy-mm-dd-hh.mm.ss.00
$SqlCmd.CommandText = $sqlSelectCVS
$SqlCmd.Connection = $SqlConnection
$SqlAdapter.SelectCommand = $SqlCmd
$SqlAdapter.Fill($export)
#Close the SQL connection
$SqlConnection.Close()
#Creates the New file with out Column header inforamtion
$export.tables[0] |export-csv -Path ($ExportTo + '\' + $ExportName) -notypeinformation
(Get-Content ($ExportTo + '\' + $ExportName)| Select-Object -Skip 1) | Set-Content ($ExportTo + '\' + $ExportName)
數據庫連接和查詢工作正常。 另外,我已經嘗試過在沒有記事的情況下在記事本中打開它,並且該日期是將來的日期。.我對powershell還是很陌生,所以如果您能解釋任何建議以便我能理解,我將不勝感激。
謝謝
您可以在PowerShell中使用.NET庫來格式化字符串,如下所示:
"{0:yyyy-mm-dd-hh.hh.mm.ss.00}" -f [datetime]$stringOrWhateverDateVariable
$ stringOrWhateverDateVariable將是SQL表中的日期變量。
我無法測試SQL datababe,但是它適用於DateTime格式的字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.