How to Convert DateTime format from "/" to "-"

Hi ,
I am getting date explicitly like (String) startDateStr= 14-03-2020 12:28:14 IST, now iam removing IST from that and storing it in cleanedStartDateStr(String)
now I want to convert that string format to DateTime format, for that I have done like below


but after converting to DateTime format I am getting result as 03/14/2020 12:28:14
but i need in a format of 14-03-2020 12:28:14 in DateTime type.
how can i fix this issue ? please help

do this @rajesh.m
startDate.toString("dd-MM-yyyy HH:mm:ss)

@rajesh.m

cleanedStartDateStr = DateTime.ParseExact(startDateStr.Replace(" IST", ""), "dd-MM-yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture).ToString("dd-MM-yyyy HH:mm:ss")

Import System.Globalization

Hi @rajesh.m

DateTime.ParseExact(Input,"MM/dd/yyyy hh:mm:ss",System.Globalization.CultureInfo.CurrentCulture).ToString("dd-MM-yyyy hh:mm:ss")

Regards,

Hi @rajesh.m

Try below expression

cleanedStartDateStr = DateTime.ParserExact(cleanedStartDateStr.Replace(“IST”,“”).Trim,“dd-MM-yyyy HH:mm:ss”,system.globlization.cultureinfo.InvariantCulture).tostring(“dd-MM-yyyy HH:mm:ss”)

Happy Automation :slight_smile:

@rajesh.m

How about the following?

Regards,

Hi @rajesh.m

You can’t get the output in DateTime variable because your preferred output is of the format dd-MM-yyyy HH:mm:ss so you will get the output dataType as System.String. If you want the output in System.DateTime DataType format variable your output will be of format MM/dd/yyyy HH:mm:ss.

startDateStr= "14-03-2020 12:28:14 IST"

Output= DateTime.ParseExact(startDateStr.ToString.Replace("IST",""),"dd-MM-yyyy HH:mm:ss ",System.Globalization.CultureInfo.InvariantCulture)

In the above case, Output will be of DataType System.DateTime


DataType:

startDateStr= "14-03-2020 12:28:14 IST"

Output= DateTime.ParseExact(startDateStr.ToString.Replace("IST",""),"dd-MM-yyyy HH:mm:ss ",System.Globalization.CultureInfo.InvariantCulture).ToString("dd-MM-yyyy HH:mm:ss")

In the above case Output will be DataType System.String


DataType:

Hope it helps!!

@rajesh.m

If you just use .ToString or print a datetime variable then it uses the default system format

If you need any specific format you need to specify that in the .ToString("dd-MM-yyyy HH:mm:ss") as you need

Cheers