[英]Date format when exported to CSV using Powershell
I Have a powershell script that executes a sql query, then exports the information to a CSV file. 我有一个Powershell脚本,该脚本执行sql查询,然后将信息导出到CSV文件。 However the CSV file reformats all DateTimes entry to
mm/d/yyyy hh:mm:ss
. 但是,CSV文件将所有DateTimes条目重新格式化为
mm/d/yyyy hh:mm:ss
。 I need the entries to be in the format yyyy-mm-dd-hh.mm.ss.00
我需要输入的格式为
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)
The database connections and query work just fine. 数据库连接和查询工作正常。 Also as a side note Ive tried opening it in notepad with no luck and the date is a future date.. I am still very new to powershell so if you could explain any suggestions so i could understand it, i would appreciate it.
另外,我已经尝试过在没有记事的情况下在记事本中打开它,并且该日期是将来的日期。.我对powershell还是很陌生,所以如果您能解释任何建议以便我能理解,我将不胜感激。
Thanks 谢谢
You can use .NET libraries in PowerShell to format the string, like so: 您可以在PowerShell中使用.NET库来格式化字符串,如下所示:
"{0:yyyy-mm-dd-hh.hh.mm.ss.00}" -f [datetime]$stringOrWhateverDateVariable
$stringOrWhateverDateVariable would be the date variable from the SQL table. $ stringOrWhateverDateVariable将是SQL表中的日期变量。
I cannot test on an SQL datababe, but it works for strings to your DateTime format. 我无法测试SQL datababe,但是它适用于DateTime格式的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.