DateTime Manipultion - UiPath
Handling Date and Time operations in UiPath RPA Tool Like a Boss
When using UiPath tool while building an RPA Solution, we all have that moment of trying to figure out how to manipulate date and time. DateTime variable and its associated functions in the UiPath tool come in handy for this. This cheat sheet covers the most used DateTime functions in UiPath. If you are looking for something simple but practical with examples, take a look!
Let’s start by learning how to initialize the DateTime variable:
dateTimeVar = New DateTime(2022, 5, 22)
Where ‘dateTimeVar’ is of DateTime datatype and the input date format must be in yyyy, MM, dd format.
Now let’s try getting the current date time and convert it into different formats:
Datetime.Now.ToString()
The output will be in dd/MM/yyyy HH:mm:ss format.
For example, 02/06/2022 20:46:45.
Considering 02/06/2022 20:46:45 as the current DateTime, please refer to the following table.
Expression |
Output |
Description |
Datetime.Now.ToString(“dd”) |
02 |
Getting the current day in digits |
Datetime.Now.Day |
2 |
Getting the current day in digits |
Datetime.Now.ToString(“dddd”) |
Thursday |
Getting the current day in text |
Datetime.Now.DayOfWeek |
Thursday |
Getting the current day in text |
Cint(Datetime.Now.DayOfWeek) |
5 |
Getting the position of the current day in a week |
Datetime.Now.ToString(“ddd”) |
Thu |
Getting only the first 3 letters of the current day |
Datetime.Now.ToString(“MM”) |
06 |
Getting the current month in digits |
Datetime.Now.Month |
6 |
Getting the current month in digits |
Datetime.Now.ToString(“MMMM”) |
June |
Getting the current month in text |
Datetime.Now.ToString(“MMM”) |
Jun |
Getting only the first 3 letters of the current month |
Datetime.Now.ToString(“yyyy”) |
2022 |
Getting the current year |
Datetime.Now.Year |
2022 |
Getting the current year |
Datetime.Now.ToString(“yy”) |
22 |
Getting only the last 2 digits in the current year |
Datetime.Now.ToString(“hh”) |
07 |
Getting the current hour in 12 hours format |
Datetime.Now.ToString(“HH”) |
19 |
Getting the current hour in 24 hours format |
Datetime.Now.Hour |
19 |
Getting the current hour in 24 hours format |
Datetime.Now.ToString(“mm”) |
46 |
Getting current minutes |
Datetime.Now.Minute |
46 |
Getting current minutes |
Datetime.Now.ToString(“ss”) |
45 |
Getting current seconds |
Datetime.Now.Second |
45 |
Getting current seconds |
Datetime.Now.ToString(“ff”) |
49 |
Getting current milliseconds |
Datetime.Now.Millisecond |
49 |
Getting current milliseconds |
Datetime.Now.ToString(“tt”) |
PM |
Getting current Datetime with AM/PM |
Datetime.Now.ToString(“HH:mm:ss K”) |
20:46:45 +05:30 |
Getting current time with Timezone
(Where represents the current timezone) |
Current Datetime in detail:
Datetime.Now.ToString(“dd/MM/yyyy hh:mm:ss ff tt K”)
Output à 02/06/2022 08:46:45 49 PM +05:30.
Add value to current date:
Datetime.Now.AddDays(2).ToString
Output à 04/06/2022 22:46:45
Here the date is incremented by 2.
AddDays is used to add days. Similarly, AddMonths, AddYears, AddHours, AddMinutes add AddSeconds are used to add months, years, hours, minutes, and seconds respectively.
Adding negative numbers will subtract based on the given input.
Datetime.Now.AddMonths(-2).ToString
Output à 02/04/2022 22:46:45
Converting a string variable into Datetime:
Convert.ToDatetime(strVar) or CDate(strVar) or DateTime.Parse(strVar)
Where ‘strVar’ is a date in string format.
Example 1:
strVar = "06/18/2022"
Output à 06/18/2022 00:00:00
Example 2:
strVar = "05 JAN 2020 05:45:13 PM"
Output à 01/05/2020 17:45:13
By using Convert.ToDateTime, CDate or DateTime.Parse, only a few date formats can be converted into DateTime type. In this, the pattern of the string cannot be defined. But there might be a scenario where defining the pattern would be necessary. This is where ParseExact comes into the picture.
Converting a string variable into Datetime using ParseExact:
Datetime.ParseExact(strVar, “dd/MM/yyyy”, System.Globalization.CultureInfo.InvariantCulture)
Using ParseExact you can convert any type of date format in string type to DateTime by defining its actual pattern.
Example 1: strVar = "06/18/2022"
Datetime.ParseExact(strVar, “MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture)
Output à 06/18/2022 00:00:00
The output will be in DateTime format.
Example 2: strVar = "05 JAN 2020 05:45:13 PM", then converting gives us 01/05/2020 17:45:13
Datetime.ParseExact(strVar, “dd MMM yyyy hh:mm:ss tt”, System.Globalization.CultureInfo.InvariantCulture)
Output à 01/05/2020 17:45:13
Checking if the given string in DateTime format is in the valid format:
DateTime.TryParseExact(strVar,“MM/dd/yyyy hh:mm:ss”, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, Nothing)
The above expression gives a Boolean as output. If the given string format matches with a given pattern, the output is true, else false.
Example 1: strVar = "06/18/2022"
DateTime.TryParseExact(strVar,“MM/dd/yyyy”, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, Nothing)
Output à true
Example 2: strVar = "05 JAN 2020 05:45:13 PM”
Datetime.ParseExact(strVar, “dd/MM/yy”, System.Globalization.CultureInfo.InvariantCulture)
Output à false
There might be a situation where the date string might be in different formats and needs to be converted into DateTime. In that case, create an array variable with all possible formats and use the below expression.
DateTime.ParseExact(strVar, dateFormats, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None)
Where ‘strVar’ is of type String and ‘dateFormats’ is an Array of String with all possible formats.
Example:
strVar = "06-18-2022"
dateFormats = {“dd/MM/yy”,”MM-dd-yyyy”,”MM/dd/yyyy”,”dd MMM yy hh:mm:ss”}
Output à 06/18/2022 00:00:00
Difference between two dates:
The difference between two dates can be calculated using the DateDiff function. It takes interval and 2 inputs in DateTime as a parameter. If the input is in string format, it needs to be converted to DateTime using ParseExact. The output will be obtained in an integer type.
For interval as Days (difference in days):
If input is DateTime type:
DateDiff(DateInterval.Day, Datetime1,Datetime2)
If the input variable is a string:
Example: strVar1 = "13-03-2022", strVar2 = "23-03-2022"
DateDiff(DateInterval.Day, DateTime.ParseExact(strVar1, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture),DateTime.ParseExact(strVar2, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture))
Output à 10
Find the difference between the input date and the current date:
Example: strVar = "22-05-2022"
DateDiff(DateInterval.Day, DateTime.ParseExact(strVar, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture),DateTime.Now)
Output à 21
Similarly, the difference can be obtained by month, year, hour, minute, second, weekday, and quarter.
Example 1: strVar1 = "13-02-2021", strVar2 = "22-05-2022"
DateDiff(DateInterval.Month, DateTime.ParseExact(strVar1, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture),DateTime.ParseExact(strVar2, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture))
Output à 15
Example 2: strVar1 = "13-02-2021", strVar2 = "22-05-2022"
DateDiff(DateInterval.Quarter, DateTime.ParseExact(strVar1, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture),DateTime.ParseExact(strVar2, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture))
Output à 5
Example 3: strVar1 = "13-02-2021", strVar2 = "22-05-2022"
DateDiff(DateInterval.Year, DateTime.ParseExact(strVar1, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture),DateTime.ParseExact(strVar2, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture))
Output à 1
Convert DateTime from local timezone to specific timezone:
Assign a variable with System.TimeZoneInfo datatype and mention the required time zone as shown below.
specificTimeZone = TimeZoneInfo.FindSystemTimeZoneById(inputTimeZone)
outputDateTime = TimeZoneInfo.ConvertTime(now, TimeZoneInfo.Local, specificTimeZone)
Where ‘inputTimeZone’ is a String variable that indicates the desired time zone and ‘outputDateTime’ is a DateTime variable that will return date and time based on the input timezone.
Example 1:
inputTimeZone = “Bangladesh Standard Time”
‘outputDateTime’ will return the current time as per Bangladesh Standard Time.
Example 2:
inputTimeZone = “Tokyo Standard Time”
‘outputDateTime’ will return the current time as per Tokyo Standard Time.
Converting Current DateTime to UTC:
DateTime.Now.ToUniversalTime()
Finding the total number of days in any month:
DateTime.DaysInMonth(inputYear, inputMonth)
inputYear = 2022
inputMonth = 5
Where inputYear and inputMonth are variables of Integer type.
Output à 31
For the current month:
DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)
Finding the first date of the current month:
New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString
Output à 06/01/2022 00:00:00
Finding the end date of the current month:
New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)).ToString
Output à 06/30/2022 00:00:00
Now let’s do the same with the previous month:
New DateTime(DateTime.Now.Year, DateTime.Now.Month,1).AddDays(-1).ToString
Output à 05/31/2022 00:00:00
This can be achieved by finding the first day of the current month and subtracting a day from the same.
Finding the end date of any month:
inputYear = 2021
inputMonth = 10
Where inputYear and inputMonth are variables of Integer type.
New DateTime(inputYear, inputMonth, DateTime.DaysInMonth(inputYear, inputMonth)).ToString
Output à 10/31/2021 00:00:00
Finding the first date of the year:
New DateTime(DateTime.Now.Year,1,1).ToString
Finding the last date of the year:
New DateTime(DateTime.Now.Year,12,31).ToString
Converting a OADate in excel to a DateTime:
DateTime.FromOADate(Convert.ToDouble(strVar))
Example: strVar = "44703"
Where ‘strVar’ is DateTime in OADate format and datatype is String.
Output: 05/22/2022 00:00:00
Getting Day of the Year:
Datetime.Now.DayOfYear
Output à 153
The below expression gives the day of the year for a specific year.
New DateTime(2021,5,22).DayOfYear
Output à 142
Checking if given year is leap year:
DateTime.IsLeapYear(inputYear)
inputYear = 2022
Output à false
Finding the first working day of the current month:
Considering Saturday and Sunday as weekends.
Enumerable.Range(1, DateTime.DaysInMonth(now.Year,now.Month)).Select(function(d) new DateTime(now.Year,now.Month,d)).where(function(x) Not(x.DayOfWeek=DayOfWeek.Saturday Orelse x.DayOfWeek=DayOfWeek.Sunday)).toList().First.ToString
Output à 06/01/2022 00:00:00
Finding the last working day of the current month:
Enumerable.Range(1, DateTime.DaysInMonth(now.Year,now.Month)).Select(function(d) new DateTime(now.Year,now.Month,d)).where(function(x) Not(x.DayOfWeek=DayOfWeek.Saturday Orelse x.DayOfWeek=DayOfWeek.Sunday)).toList().Last.ToString
Output à 06/30/2022 00:00:00
Finding the first and last occurrence specific day in a current month:
For example, let’s find the first and last Thursday in a current month,
Enumerable.Range(1, DateTime.DaysInMonth(now.Year,now.Month)).Select(function(d) new DateTime(now.Year,now.Month,d)).where(function(x) x.DayOfWeek = DayOfWeek.Thursday).toList().first.ToString
Output à 06/02/2022 00:00:00
Enumerable.Range(1, DateTime.DaysInMonth(now.Year,now.Month)).Select(function(d) new DateTime(now.Year,now.Month,d)).where(function(x) x.DayOfWeek = DayOfWeek.Thursday).toList().last.ToString
Output à 06/30/2022 00:00:00
Similarly, we can find the first and last day or working day of any month by replacing now.Year and now.Month by specific year and month.
Trying out different expressions:
DateTime.Now.ToOADate à 44714
DateTime.Now.ToLongDateString() à Thursday, 02 June 2022
DateTime.Now.ToShortDateString() à 06/02/2022
DateTime.Now.ToLongTimeString() à 20:46:45
DateTime.Now.ToShortTimeString() à 20:46
DateTime.Now.Date() or DateTime.Now.Today() à 06/02/2022 00:00:00
DateTime.Now.GetDateTimeFormats() à This expression will return an Array of String with different date formats.
I hope this will come in handy. Save it for future reference.
Happy Automation.