我无法理解如何在 PowerShell 中格式化字符串中的日期时间变量。
$startTime = Get-Date
Write-Host "The script was started $startTime"
# ...Do stuff...
$endTime = Get-Date
Write-Host "Done at $endTime. Time for the full run was: $( New-TimeSpan $startTime $endTime)."
在我想要 ISO 8601 时给我美国日期格式.
我可以使用
$(Get-Date -Format u)
但我想使用 $endTime 来正确计算时间跨度。
我已经尝试了 $
、(
、)
、endTime
、-format 的所有排列
、u
、.ToString(...)
和 .ToShortDate()
,但最有效的那个。
最佳答案
"This is my string with date in specified format $($theDate.ToString('u'))"
或
"This is my string with date in specified format $(Get-Date -format 'u')"
子表达式 ($(...)
) 可以包含任意表达式调用。
微软文档standard and custom DateTime
format strings .
https://stackoverflow.com/questions/10025333/