Wondering, is that a regex in powershell can replace date from dd-mon-yy to dd/MM/yy
Example: 25-FEB-16 change to 25/02/16
You should use [DateTime]::ParseExact()
as regex would require 12 different replace-operations, or a MatchEvalutor
to convert the month.
Example using regex MatchEvaluator
:
$MatchEvaluator = {
param($match)
#Could have used a switch-statement too..
$month = [datetime]::ParseExact($match.Groups[2].Value,"MMM",$null).Month
"{0:00}/{1:00}/{2:00}" -f $match.Groups[1].Value, $month, $match.Groups[3].Value
}
[regex]::Replace("25-FEB-16","(\d+)-(\w+)-(\d+)", $MatchEvaluator)
25/02/16
Looking at that, I would say that using only ParseExact()
is a much better solution:
try {
[datetime]::ParseExact("25-FEB-16","dd-MMM-yy", $null).ToString("dd/MM/yy", [cultureinfo]::InvariantCulture)
} catch {
#Invalid date-format
}
25/02/16
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments