Regular expression convert Date format from dd-mon-yy to dd/MM/yy

user664481

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

Frode F.

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.

edited at
0

Comments

0 comments
Login to comment

Related

puzzled with how to convert ddmmyy string to dd-MMM-yy date

Convert date from DD-MON-YY to YYYYMMDD

Regular expression for "mm-dd-yy" date format not finding any match in python

How to change/convert input date format from MM dd yy to yyyy-mm-dd on submit

Javascript convert date format from "dd/mm/yy" to "mm/dd/yyyy"

Sql Server Date Format DD-Mon-YY

How can verify if a date is in the format dd mon ' yy in Ruby?

Convert string of dd-MON-yy to date in Python

The simplest way to convert a long date format to YY/MM/DD HTML

How to convert the given date to dd-mm-yy format in javascript?

How to convert date format "dd/mm/yy" to "yy/mm/dd" for mysql database inserting?

How to convert date dd/mm/YY to mm-dd-YY format in PHP

Date format dd/mm/yy

Changing date format from YY WW to DD-MM-YYYY

pyspark sql convert date format from mm/dd/yy hh:mm or yyyy-mm-dd hh:mm:ss into yyyy-mm-dd hh:mm format

Excel - Convert date from yymmdd to dd/mm/yy

GoogleFinace convert dd/mm/yy to yy,mm,dd format

how to convert sql date formate unix time stamp to date format as dd mm yy format?

Convert MySQL date format to DD/MM/YY date format using Javascript

How to convert date in format mm/yy from character to date class?

Convert date with format dd.mm.yy to yyyy-mm-dd in python

want to convert string mm/dd/yy to date yyyy/mm/dd format in Sybase

SQL convert date dmmyyyy and ddmmyy to yyyy-mm-dd

Regular expression for date format MM/DD/YYYY returns false results

What is the regular expression for Date format dd\mm\yyyy?

Android - change Date Format to dd/mm/yy

SAS numeric date format to dd/mm/yy

Converting string format dd - mmm - yy to date

oracle convert DD-MON-YY to DD/MM/YYYY