Convert date of string format to date time

hiii guys,i have a query,say i have a string like strdate=“01/20/2019” and i just tried it to convert it into date time format like **Convert.todatetime(strdate)**as expected it got converted into datetime format but when the strdate=“20/01/2019” it is saying string was not recognized as valid date time. now my question is it convert into datetime format only when the **strdate=“01/20/2019”**or if it possible if it is in **str=“20/01/2019”**format plz help me out from this.thankyou

1 Like

Hi @venkatmalla6

Use cdate (strdate).Tostring (“MM/dd/yyyy”)

Thanks
Ashwin.S

1 Like

Hi buddy @venkatmalla6
If the format is "01/20/2019"
Then use this
In_string = "01/20/2019"
Out_datetime = Datetime.ParseExact(In_string.ToString,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

Or if it is in this format
In_string = "20/01/2019"
Out_datetime = Datetime.ParseExact(In_string.ToString,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

This will work for sure buddy
Cheers @venkatmalla6

2 Likes

@Palaniyappan hiii i tried second one it is giving me the output as 01/20.2019.

Can i have a view on screenshot of that buddy
Cheers @venkatmalla6

@Palaniyappan sure buddy,

I think it works fine buddy, we are able to convert right
Any issue
Cheers @venkatmalla6

@Palaniyappan yeah buddy it works but the thing is the format of date has changed.actually i gave my string as 20/01/2019.the output which i want is 20/01/2019 in datetime format but it is giving me as 01/20/2019.

And one more doubt if i have a string like this 20JUNE2019.can we make it as 20/06/2019 in dateformat.

In that case we can mention the format we want in ToString ()
Like this
In_string = "20/01/2019"
Out_datetime = Datetime.ParseExact(In_string.ToString,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture). ToString (“dd/MM/yyyy”)

Yes of course for this
Out_datetime = Datetime.ParseExact(In_string.ToString,“ddMMMMyyyy”,System.Globalization.CultureInfo.InvariantCulture). ToString (“dd/MM/yyyy”)

Well for this once when a string is converted to Datetime we dont need to worry on the format, dot net will take that as Datetime in general and compare with any Datetime value
Only when we enter the Datetime as string to a field we need to work on format needed
. cheers @venkatmalla6

@Palaniyappan ok thanks buddy.

Any issues Still
Did thia get resolved
Cheeers @venkatmalla6

@Palaniyappan yeah buddy and one more thing yesterday i asked a query on writing multiple sheets in excel i just shared my zip file already if you don’t mine can you have a look.the thing is it is not scraping the data as expected in excel sheet,plz have a look at my excel file in that and also have a look at my xaml file also.and thanks for your valuable time spending on this.

Hi there!

Appreciate your assistance with an issue that i am facing.

I have managed to extract a date from an email which is in this format, “04 November 2019” and have already converted in to a string and assigned it to a variable, DateA.

My question here is, how can i convert the variable, DateA, to specific format lets say “dd-mm-yyyy”. Tried the Datetime.ParseExact method but the error message “String was not recognized as a valid DateTime”.

I’m guessing that “04 November 2019” is not a valid DateTime? Is there any work arounds for this? Looking forward to your response buddy!

1 Like

@R11

Welcome to the uipath community.

Try below expression and it will give you required date.

   String requiredDate = DateTime.ParseExact(yourDate, "dd MMMM yyyy", System.Globalization.CultureInfo.InvariantCulture).Tostring("dd-MM-yyyy")

Here, yourDate = “04 November 2019”

6 Likes

Really appreciate your prompt response Mr Lakshman! And great news, this worked perfectly! Wishing you a great week ahead! Cheers :slight_smile:

3 Likes

@R11

Glad to help you. Happy Automation :grinning:

1 Like

I could use some help because I must be doing something wrong.

I have string variable pdate that I am creating from a transaction item that is a date in format yyyy-MM-dd

I need to actually get it into date format so I have an assign for a variable pdateNew that I have has type DateTime.

I am using this code to try and convert, I want to get it to MM/dd/yyyy

pdateNew=
Datetime.ParseExact(pdate.ToString,“yyyy-MM-dd”,System.Globalization.CultureInfo.InvariantCulture). ToString (“MM/dd/yyyy”)

I am getting the error: “Cannot assign from type System.String to type System.DateTime in Assign Activity Assign”

Would appreciate any help. Thanks!

@Chris_Livesay If you want only MM/dd/yyyy format you can get it in the form of String , But in the form of Date , I don’ think it’s possible

In your Assign Statement Dont use ToString(“MM/dd/yyyy”) since your variable is of DateTime it cannot hold a string value

The format is not critical, I can have it be something different but I need a valid date format because I am going to have to do a lesser than, greater than comparison to another date.