Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting DateTime (Format$(Now,"dd/mm/yyyy hh:mm:ss")

Status
Not open for further replies.

Frink

Programmer
Mar 16, 2001
798
GB
Hallo,

How do I output the current Date and Time formatted how I want?
In VBA I'd use the Format$ function defined in the subject?
The code I have so far is:
Code:
$datDateTime=Get-Date
$strDateTime=""
$strDateTime=$strDateTime$datDateTime.Day
$strDateTime=$strDateTime"/"$datDateTime.Month
$strDateTime=$strDateTime"/"$datDateTime.Year
etc.
write-host "The current Date and Time is: "$strDateTime"."
The current Date and Time is: 12/2/2008.
(As you may be able to tell I'm a n00b with my roots in VBA - elementary my dear Watson)
The problem is, I want the month as 2 digits.
The help I have found gives information like:
Code:
"0:D2" -f $datDateTime.Month
But how do I include that in my $strDateTime code?
Code:
$strDateTime=$strDateTime"/""0:D2" -f $datDateTime.Month
Gives an error:
Unexpected token '/"{0:D2}' in expression or statement.

Any ideas?

- Frink

Is it just me, or is this getting worse...
 
Hallo again,

I think I have it now:
Code:
$strDateTime="{0:D2}" -f $datDateTime.Day+"/"+"{0:D2}" -f $datDateTime.Month+"/"+"{0:D4}" -f $datDateTime.Year+" "+"{0:D2}" -f $datDateTime.Hour+":"+"{0:D2}" -f $datDateTime.Minute+":"+"{0:D2}" -f $datDateTime.Second
It works, but it's nasty! Any other ideas how this could be done more succinctly?
What's the deal with concatenating strings? It seems like sometimes you need a +, other times you don't.

- Frink
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top