String is not converting to Valid date

My date is 2-1-2020 (2nd Jan 2020), I m struggling to convert it to Date format as after converting this I need to get which day falls on this date (That Solution i have)

So my main concern is how to convert this string to date.

Please note my system setting or date format is mm/dd/yyyy so today is 01/22/2020 for me.

"Fixed Date : " + Datetime.parseexact(“1-2-2020”,“MM-dd-yyyy”,system.globalization.cultureinfo.invariantculture).ToString(“dd/MM/yyyy”)

Really need help as I tried all methods mentioned here at UiPath forum. :frowning:

Thanks

1 Like

@Anil_choudhary You have to change the format within your parseexact. You should change it to “d-M-yyyy”

Here is the list of all the datetime formats for future reference :slight_smile:
https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

EDIT: See @bcorrea’s comment below :slight_smile:

i think it is d-M-yyyy tho… if the string is 2-1-2020

1 Like

Well its d-M-yyyy only but not getting loaded to date directly and whenever loading it using convert.todatetime its converted as m-d-yyyy which is wrong.
Inputfile.txt (1.4 KB)

I want to read the column 8 (datum) of this file. it will be really helpful.

After reading the date i need to find the day of it using dayofweek

Thanks

converting to datetime, carries no format, it is an object… [
DateTime.DayOfWeek

Main.xaml (11.7 KB)

Not sure what I m trying but this is what i had written

Last resort is to cut the values into individual numbers and then add them again to generate a new date :slight_smile:

@Anil_choudhary Can you please copy+paste the actual string you are trying to convert? The datetime.ParseExact should be working based on what you’ve posted.

Keep in mind that the system setting or date does not matter in this case at all

Hi Dave,

I had uploaded the I put file in earlier post which I m trying to read. I tried all sort of things and than only posted the new channel.

Thanks

@Anil_choudhary - I apologize, I didn’t see that file. The text file you uploaded looks like a semi-colon delimited CSV file. Are you using the ‘read csv’ activity to convert it to a datatable, then using datatable.row(0).item(6).tostring to get the date as a string value? If so, I believe that is your problem. The ‘read csv’ activity is going to be converting it to a datetime automatically. If you don’t specify a culture at the top of your workflow it is going to use an invariant culture which is basically US culture meaning it automatically reads it in “M-d-yyyy” format. Instead, try changing the culture BEFORE you read the CSV. It’s not guaranteed to work, but it may cause it to read the CSV with the correct format.

Based on the CSV headers I’m guessing this is dutch? If so, try putting this assign at the top of the sequence before reading CSV. Assign CultureInfo.CurrentCulture = new CultureInfo("nl-NL")

If that doesn’t work we can explore other options, but it may be more difficult

Hi
kindly try with this expression
if the date is stored ina string variable named strinput
then the expression be like this, which is applicable for any date with - in between them
in assign activity

strinput = Split(strinput,"-")(0).ToString.PadLeft(2,CChar(“0”))+"-"+Split(strinput,"-")(1).ToString.PadLeft(2,CChar(“0”))+"-"+Split(strinput,"-")(2).ToString

then in your string like this

"Fixed Date : " + Datetime.parseexact(strinput.ToString,“MM-dd-
yyyy”,system.globalization.cultureinfo.invariantculture).ToString(“dd/MM/yyyy”)

Cheers @Anil_choudhary

1 Like

Let me try this.
Yes the language is Dutch and the file is semicolon separated.
Yes you are correct I m reading the csv and then looping through all records and use to generate the output for one line at a time.

Will try the method and will update you about progress.

Thanks

Yes that’s what I want to use as last option. Check if the length is not less than 8 characters. If its 8 or more characters split for - and generate a new string in whatever date format I need.

Well that’s a lot for the format or string to generate it.

Thanks

@Anil_choudhary I tried using the ‘read csv’ activity on your file and used the following write line activity: Write Line: dt1.Columns(7).DataType.ToString This came back saying it was system.string datatype. That means the cultureinfo piece is not necessary.

I then tried simply using DateTime.ParseExact and it worked perfectly, so I’m not sure which part you’re having trouble with? Here are my exact steps:

  1. Read CSV --> output as dt1
  2. Write Line: Datetime.ParseExact(dt1.Rows(0).Item(7).ToString,"d-M-yyyy",new CultureInfo("nl-NL")).ToString("dd/MM/yyyy")

That’s it! Just 2 steps and it worked perfectly. I also did a for each row loop and it worked for each row in the CSV

EDIT: here is the file if you’d like to look. I just added my own sequence above your old sequence. Main.xaml (16.9 KB)

yah that would work for sure
@Anil_choudhary

1 Like

Hi Dave,

Will check and thanks a lot for the workflow.

Will update with the issue if any.

Thanks

hi @Dave

Its working perfectly and thanks a lot for your help.
Thanks a lot for teaching about new CultureInfo("nl-NL")

Thanks

1 Like

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