[英]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.