2012-04-05 15 views
21

Nie mogę zrozumieć, jak formatowanie zmiennej datetime w ciągu znaków działa w PowerShell.Formatowanie PowerShell Get-Date wewnątrz łańcucha

$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)." 

podaje mi amerykański format daty, podczas gdy chcę ISO 8601.

mogę użyć

$(Get-Date -Format u) 

ale chcę użyć $ EndTime aby obliczenie przedziału czasu poprawne.

Próbowałem wszystkich permutacji $, (,), endTime, -format, u, .ToString (...) i .ToShortDate(), ale ten, który działa.

Odpowiedz

32
"This is my string with date in specified format $($theDate.ToString('u'))" 

lub

"This is my string with date in specified format $(Get-Date -format 'u')" 

sub-wyrażenie ($(...)) może obejmować dowolne wyrażenia połączenia.

MSDN Dokumenty zarówno standard and custom DateTime format strings.

4

Zamiast string interpolation można po prostu sformatować DateTimeToString("u") użyciu metody i CONCATENATE że z resztą wyrażenie:

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

Prawidłowe rozwiązanie. Brakuje jednak "ciągu wewnątrz struny". Nie ma/rzeczywistego/powodu, dla którego nie będę się łączyć, zamiast łańcucha string-inside-string. – LosManos

+0

Absolutnie poprawne. @ Richard odpowiedź jest najlepsza. –

25

Można użyć operatora -f

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

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 
+0

Strony MSDN dla [standard] (http://msdn.microsoft.com/en-us/library/az4se3k1.aspx) i [niestandardowe] (http://msdn.microsoft.com/en-us/library/ 8kb3ddd4.aspx) ciągi formatów mają mnóstwo przykładów, nawet w różnych językach :). +1 za sugerowanie ciągów formatów, które moim zdaniem są tutaj preferowane. – Joey

+0

$ a staje się ciągiem, n'est-ce pas? Potrzebowałem prawdziwego obiektu typu string. Sposób, aby najpierw ustawić formatowanie, a następnie wartość była dla mnie nowa. Dzięki. – LosManos

+0

Dzięki @ Joey, nie mogłem położyć palca na tych stronach, więc kopiuję wklej jeden z moich dokumentów. – JPBlanc

Powiązane problemy