bndit
asked on
Powershell: Padding a string
I'm reading a csv file that has already process a date in the format MMDDYYYY. However, for single-digit months, there is no leading 0. I'd like to "pad" this string so that there's a leading 0.
I know I can do something like this
I'm looking for the most efficient way to do this (less code as possible)
Thx
I know I can do something like this
$file = import-csv c:\temp\myfile.csv
foreach ($line in $file) {
$date = $line.DATE
if($date.Length -lt 8){
$date = -Join ("0",$date)
} else {
# date length is good; do nothing
}
}
I'm looking for the most efficient way to do this (less code as possible)
Thx
ASKER
what am I doing wrong? See snapshot attached to see how I'm running it (straight on the shell)
Exception calling "Parse" with "1" argument(s): "String was not recognized as a valid DateTime."
At line:1 char:18
+ [DateTime]::Parse <<<< ($date).ToString("MMDDYYYY ")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
date.png
Exception calling "Parse" with "1" argument(s): "String was not recognized as a valid DateTime."
At line:1 char:18
+ [DateTime]::Parse <<<< ($date).ToString("MMDDYYYY
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
date.png
sorry for the confusion, parse and parseExact can't deal with single digit month even when explicitly defined:
also in several places i found that ppl use similar methods to detect single digits months (or days) and simply pad with 0 to work with DateTime objects.
you can shorten your line by doing this:
$date = "6102013"
$date = [DateTime]::ParseExact($date, "Mddyyyy",[System.Globalization.CultureInfo]::CurrentCulture)
also in several places i found that ppl use similar methods to detect single digits months (or days) and simply pad with 0 to work with DateTime objects.
you can shorten your line by doing this:
$file = import-csv c:\temp\myfile.csv
foreach ($line in $file) {
[DateTime]$date = @{$true=("0"+$line.DATE);$false=$line.DATE}[$line.DATE.Length -lt 8]
}
ASKER
This article would make me think I could use the static method ParseExact
http://dusan.kuzmanovic.net/2012/05/07/powershell-parsing-date-and-time/
do you know why it's not working?
http://dusan.kuzmanovic.net/2012/05/07/powershell-parsing-date-and-time/
do you know why it's not working?
I tested parseExact it wont work with single digit month
How about
foreach ($line in import-csv c:\temp\myfile.csv) {
$date = $line.DATE.PadLeft(8, '0')
}
orimport-csv c:\temp\myfile.csv | % { $date = $_.Date.PadLeft(8, '0') }
ASKER
Thanks for trying, but I think it's going to be really hard to convert dates given to me in this format since a 1-digit month and 1-digit day are used. Padding left would yield incorrect dates where the date is 1-digit (i.e. 1271950 would yield 01271950 but I really want 12071950).
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
length and stuff:
Open in new window