Problem with date formatting

Hello!

I have string which is date in format “Dec-15-2020” , I want turn it into “Dec 15, 2020”

It can also be “Jan-1-2021” and then it has to be “Jan 1, 2021”

So I want replace first “-” with empty space " " and 2nd “-” with , and empty space ", "

What is best way to do this?

1 Like

Use DateTime.ParseExact

The syntax is:
DateTime.ParseExact(dateString, currentFormat,Nothing).ToString(newFormat)

For example if your date is “Dec-15-2020” and you want to convert it to Dec 15, 2020
you do
DateTime.ParseExact("Dec-15-2020","MMM-dd-yyyy",Nothing).ToString("MMM dd, yyyy")

you can even add hours/minutes/seconds
hours = hh
minutes = mm (note that upper case M = month, lower case m = minutes)
seconds = ss

This is just a few of the choices, you can see the full list here

1 Like

Hi,

There are some ways to achieve it as the following.

Regex

System.Text.RegularExpressions.Regex.Replace(text,"-(\d+)-"," $1,")

ParseDateTime

DateTime.ParseExact(text,"MMM-d-yyyy",nothing).toString("MMM d,yyyy")

Split

arrStr = text.Split({"-"c})
arrStr(0)+" "+arrStr(1)+","+arrStr(2)

Regards,

1 Like

Hello @Darba

Check below for your reference
https://forum.uipath.com/t/convert-date-time-format-tutorial/242670/2

Hope this helps you

Thanks

1 Like

I tried make next edition of this by having datatable which has “Opening date” and which I loop through and change every row opening date to this format, but I got error “Assign: String was not recognized as a valid DateTime.”

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.