DateTime Format Not Working

Hi All,

I want to convert my Date strStartDate:01/7/2023 into 01/07/2023.

I am using below format however its not working.

DateTime.ParseExact(strStartDate,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

@marina.dutta

Please try this

DateTime.ParseExact(strStartDate,"dd/M/yyyy",System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy")

Cheers

Use this syntax:

Hi @marina.dutta

DateTime.ParseExact(strStartDate,“dd/M/yyyy”,System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy")

Store it an string variable.

Regards,

1 Like

Hi @marina.dutta

Try with

DateTime.ParseExact("01/7/2023",{"dd/M/yyyy", "d/M/yyyy"},System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy")

Regards

Hi,

FYI, another approach using regex.

System.Text.RegularExpressions.Regex.Replace(strStartDate,"(?=\b\d\b)","0")

Regards

1 Like

@Anil_G

my strStartDate is of String type. When I am using

DateTime.ParseExact(strStartDate,“dd/M/yyyy”,System.Globalization.CultureInfo.InvariantCulture).ToString(“dd/MM/yyyy”)

It throws me an error.
image

@marina.dutta

StrStartdate is string …and what is the datatype of the variable in assign on the left side? It should be string too

Cheers

Hi @marina.dutta

Use this syntax and store the output in string variable

DateTime.ParseExact(strStartDate.ToString,“dd/M/yyyy”,System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy")

Regards,

1 Like

Hey @marina.dutta ,
Below is the code
DateTime.ParseExact(strStartDate,"dd/M/yyyy",System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy")

Below is the screenshot of the output
image

below is the file for your refernce
Testt.zip (10.4 KB)

Hope it helps you

1 Like

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