powershell - 在字符串中格式化 PowerShell Get-Date

我无法理解如何在 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/

相关文章:

date - 将日期格式更改为 "%d/%m/%Y"

c++ - 如何计算具有 n 位小数的 float

c# - 在 Console.WriteLine 方法中格式化字符串

unicode - 如何破解 GHCi(或 Hugs)以便打印未转义的 Unicode 字符?

java - 有什么方法可以使用 Eclipse 在 Java 字符串文字中编写(复制粘贴)格式良好

formatting - 同时在多行中添加空格 - 在 Pycharm 中

formatting - htop 输出到人类可读文件

html - float 子元素 : overflow:hidden or clear:both?

css - 自定义 .sass 的格式输出?

javascript - Chart.js 数字格式