How manipulate date

Hi, I need help.

How can I manipulate a date to be one working day before the date saved in the UiPath variable?

I saved data in a variable “data_saved”, for example, and this value of data_saved is not a fixed value, this value will vary according to the reading of a pdf file that will be executed.

//

Hi @helemalmeidati ,

Let us know the datatype of the data_saved variable is a Date type or a String type.

If it is a Date type, we could use the below Expression to get the previous day :

data_saved.AddDays(-1)

@helemalmeidati

Convert.ToDateTime(data_saved).AddDaya(-1)

Regards,
Arivu

Hi @helemalmeidati

You can try like this

DateTime.ParseExact(data_saved,{“dd.Mm.yyyy”,”MM/dd/yyyy hh:mm:ss”,”MM/dd/yyyy”,”dd/MM/yyyy hh:mm:ss”}, System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.none).AddDays(-1).ToString(“dd.MM.yyyy”)

Regards
Sudharsan

@helemalmeidati

I guess you are looking for woeking days alone…

Please try this

Date_Saveddate = cdate(data_save) - this will convert string to date

Now use the below to find the previous working day

Prev_date = date_Saveddate.AddDays(if(date_saveddate.dayofweek.ToString.Tolower.Equals("monday"),-3, if(date_saveddate.dayofweek.ToString.Tolower.Equals("sunday"),-2,-1)))

Cheers

This example is not working for what I need.

My variable is a substring and I need a working day of the week and not holidays… I tried .add(-1) but there are cases where the day is a holiday. I need it to be one business day before the date saved in the variable.

Sorry if it’s not clear. If needed, try again.

Will this work to find previous business day?

It doesn’t work because not every day before is a business day

Not every day before is a business day…

@helemalmeidati

This is for previous businessday assuming you mean only monday to friday

Cheers

Hi @helemalmeidati,
Try below code to get previous day

DateTime prevDay = date.AddDays(-1)
while (prevDay.DayOfWeek == DayOfWeek.Saturday || prevDay.DayOfWeek == DayOfWeek.Sunday)
    {
        prevDay = prevDay.AddDays(-1)
    }

Regards,
Arivu

Hey try this
assign in datetime variable
datetime.parse(string variable).tostring(“dd:mm:yyy”).

I think I expressed myself wrong and I already apologize. I get the date from a pdf file, I need to handle it when inserting, currently it looks like this: Split((Split(pdf,“DATE ​​DATE”)(1).ToString),“V”)(0).ToString

I need to transform this so that it is always on a working day, and I don’t know how to do it, your example would serve me in a datatime, but it doesn’t apply or at least I can’t apply it to my need

I think I expressed myself wrong and I already apologize. I get the date from a pdf file, I need to handle it when inserting, currently it looks like this: Split((Split(pdf,“DATE ​​DATE”)(1).ToString),“V”)(0).ToString

I need to transform this so that it is always on a working day, and I don’t know how to do it, your example would serve me in a datatime, but it doesn’t apply or at least I can’t apply it to my need

@helemalmeidati

So are you getting a date from that split?and you wnat to check if it is a working day or not?

If yes then use a if condition with

cdate(data_save).DayOfWeek.ToString.Tolower.Equals("sunday") Or cdate(data_save).DayOfWeek.ToString.Tolower.Equals("saturday")

On then side you have the weekend and else you have weekdays

Cheers

Unfortunately I couldn’t do it, I’m still researching and testing.

I was wondering if it’s possible to create a variable that checks for holidays, another variable that checks if it’s Saturday and another variable to check if it’s Sunday.

I would do an if on these variables and subtract one day. Is this possible?

@helemalmeidati

Yes use the conditions provided above in separate if conditions and accordinglt subtract a day or two …

Date_save.adddays(-1) will subtract one day

Cheers

@helemalmeidati ,

I have created a workflow, as I thought it was a bit tricky to understand, as we would be needing the closest working date before the extracted date. Check the below workflow :
GetPreviousWorkingDay.zip (3.0 KB)

A few important/short summary of workflow is provided below. As I think the workflow is a bit too long, others could also have a look on these methods and maybe would be able to provide a better/ shorter approach.

  1. To Check for holidays, you would require to have the list of holiday dates according to your/Client calendar and then use it to check if the date extracted/received is a holiday date. Creating a list as shown below :

  2. To Check for weekends also you would require to have the list of weekend dates. So that you will be able to exclude the dates at the end when calculating the Working dates alone. To Calculate the weekend dates, we can use the below Expression which will calculate the weekend dates from the 1st day of the year upto the current day.

WeekendDatesUptoCurrentDate = Enumerable.Range(0,DifferenceInDays).Where(Function(x){"Sunday","Saturday"}.Any(Function(we)FirstDayOfYear.AddDays(x).DayOfWeek.ToString.Equals(we))).Select(Function(x)FirstDayOfYear.AddDays(x).Date).ToArray

Here, DifferenceInDays is calulated as from 1st of year to Current Date, WeekendDatesUptoCurrentDate is an Array of DateTime variable.
image

  1. Next, the weekend dates and the holiday dates calculated are concatenated into one list, TotalNonWorkingdates and is used to calculate the TotalWorkingDates.
    image

  2. We can now find the closest working date before the extracted date using the below expression :

TotalWorkingDates.Where(Function(x)x<CDate(extractedDate)).OrderBy(Function(x)Math.Abs((x-CDate(extractedDate).AddDays(-1)).ToTalDays)).Select(Function(x)x).First

Here, the extractedDate is your date extracted from PDF. We would also need to know the format of the date, as it was assumed it will be in the format MM/dd/yyyy.

Visuals of the working :
image

Let us know if you were able to understand the workflow and able to get the required output for the extracted date received.

My version of UiPath is free and your project does not open to run, but I managed to understand, I will try to apply. Thank you very much!

1 Like

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