格式化PowerShell获取字符串中的日期

格式化PowerShell获取字符串中的日期,powershell,formatting,Powershell,Formatting,我无法理解如何在PowerShell中格式化字符串中的datetime变量 $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 $end

我无法理解如何在PowerShell中格式化字符串中的datetime变量

$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)."
在我需要的时候给我美国日期格式

我可以用

$(Get-Date -Format u)
但是我想使用$endTime来正确计算时间跨度

我尝试了
$
结束时间
-格式
u
.ToString(…)
.toSortDate()
,所有的排列方式,但是有效的方法。

您可以使用该方法格式化字符串,并将其与字符串的其余部分连接起来:

$startTime = Get-Date
Write-Host "The script was started " + $startTime.ToString("u") 

您可以使用-f运算符

$a = "{0:D}" -f (get-date)
$a = "{0:dddd}" -f (get-date)


子表达式(
$(…)
)可以包含任意表达式调用


MSDN记录了这两种情况。

有效的解决方案。但缺少“字符串中的字符串”。对于我来说,没有/real/理由不使用string-inside-string来连接。和格式字符串的MSDN页面有很多示例,即使是在不同的语言中:)+1用于建议我认为在这里更可取的格式字符串。$a变成字符串,n'est-ce pas?我需要一个真正的字符串对象。首先设置格式,然后设置值的方法对我来说是新的。谢谢。谢谢@Joey,我只是无法将手指放在这些页面上,所以我复制粘贴了我的一个文档;其中,对于原始日期字符串文字20180612,此处所需的日期格式为yyyy mm dd。对可排序的ISO使用{0:u}。字符串在DateTime的世界中传播,并以正确的格式显示为字符串。可绕过的?坦率地说,所有这些都有点像皮塔。
Spécificator    Type                                Example (with [datetime]::now)
d   Short date                                        26/09/2002
D   Long date                                       jeudi 26 septembre 2002
t   Short Hour                                      16:49
T   Long Hour                                       16:49:31
f   Date and hour                                   jeudi 26 septembre 2002 16:50
F   Long Date and hour                              jeudi 26 septembre 2002 16:50:51
g   Default Date                                    26/09/2002 16:52
G   Long default Date and hour                      26/09/2009 16:52:12
M   Month Symbol                                    26 septembre
r   Date string RFC1123                             Sat, 26 Sep 2009 16:54:50 GMT
s   Sortable string date                            2009-09-26T16:55:58
u   Sortable string date universal local hour       2009-09-26 16:56:49Z
U   Sortable string date universal GMT hour         samedi 26 septembre 2009 14:57:22 (oups)
Y   Year symbol                                     septembre 2002
Spécificator    Type                       Example      Output Example
dd              Jour                       {0:dd}       10
ddd             Name of the day            {0:ddd}      Jeu.
dddd            Complet name of the day    {0:dddd}     Jeudi
f, ff, …        Fractions of seconds       {0:fff}      932
gg, …           position                   {0:gg}       ap. J.-C.
hh              Hour two digits            {0:hh}       10
HH              Hour two digits (24 hours) {0:HH}       22
mm              Minuts 00-59               {0:mm}       38
MM              Month 01-12                {0:MM}       12
MMM             Month shortcut             {0:MMM}      Sep.
MMMM            complet name of the month  {0:MMMM}     Septembre
ss              Seconds 00-59              {0:ss}       46
tt              AM or PM                   {0:tt}       ““
yy              Years, 2 digits            {0:yy}       02
yyyy            Years                      {0:yyyy}     2002
zz              Time zone, 2 digits        {0:zz}       +02
zzz             Complete Time zone         {0:zzz}      +02:00
:               Separator                  {0:hh:mm:ss}     10:43:20
/               Separator                  {0:dd/MM/yyyy}   10/12/2002
"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')"