How to add dates to a string date, error: String was not recognized as a valid DateTime occurs

Hi,

I have an Excel file where I have a date in format ”02.10.2015” (dd.MM.yyyy) as a string.
I need to input that date to a website but I also need to add some dates to that date.

For example, first I need to input the actual date ”12.10.2015” and second I want input a day after it like ”13.10.2015”.

I use get row item to get the first date from an excel, save the output to ”mydate” variable and then I use it straight in type into activity. After that I want to have a day after that so I use the assign activity to change date from a string to DateTime like this:

mydate = DateTime.ParseExact(mydate, “dd.MM.yyyy”, System.Globalization.CultureInfo.InvariantCulture).ToString

secondDate = DateTime.ParseExact(mydate, “dd.MM.yyyy”, System.Globalization.CultureInfo.InvariantCulture).AddDays(1).ToString

But it doesn’t work and it gives me this error sign : ” Assign: String was not recognized as a valid DateTime. ”

Both of the variable types are GenericValue, but when I change them into DateTime -type the error ”Option Strict On disallows implicit conversions from ‘String’ to ‘Date’" occurs……

Any tips or ideas why this doesn’t work? Or if there are some better ways to do it, I would be more than grateful to hear :blush: Thank you already!

BR,
Elina

Is mydate already instantiated as a String pulled from Excel before you run the first assignment? If not, then ParseExact will not work.

1 Like

Yeah mydate is actually used several times before I want to add dates to it. So would you suggest to take the date out again by get row item and save it for another variable and use that ParseExact sentence, would it help? Or do you think there is there easier way to do this? thanks :slight_smile:

If you read the data from the row field into mydate beforehand, it should be fine, as long as mydate is a String variable.

2 Likes

When I only changed mydate to string, unfortunately it still gives me this error: Assign: String was not recognized as a valid DateTime.

On which Assign activity is this error occurring?

In this second assign activity:

secondDate = DateTime.ParseExact(mydate, “dd.MM.yyyy”, System.Globalization.CultureInfo.InvariantCulture).AddDays(1).ToString

Oh, I see what’s happening. In your first assignment to mydate, change .ToString to .ToString("dd.MM.yyyy").

2 Likes

Oh it helped, thank you a lot. Still now it is giving me the date like this. 12/31/2014 00:00:00

I would hope to get is as 12.31.2014 and no need for the hours. I wonder why it does that since in that assign activity we used “dd.MM.yyyy”.

Datetimes will always include the time, but if you need to get just the date as 12.31.2014, you can always convert that date with .ToString("MM.dd.yyyy").

1 Like

Oh sorry, I meant it as “dd.MM.yyyy”. but since I forgot to put .ToString("dd.MM.yyyy") into another assign activity I had it in the wrong (12/31/2014 00:00:00) format. But anyhow that works now!!! Thank you a lot for your help (I’ve been struggling with this the whole day so this really made my day) thanks. :slight_smile:

1 Like