UiPath

Introduction to date and time manipulation in UiPath with samples

In UiPath Studio development, there are many times when you need to manipulate dates and times.

In this article, I will show you how to specify the DateTime type of a variable, get the current date and time, calculate the time difference, get the end of the month or the first day of the month, convert between Western and Japanese calendars, check the Date type, and convert between Date and string types with samples.

 

 

 Related Articles Create a practical UiPath robot with Udemy’s online course

 

The operator of this blog, F-penIT blog

 

This site was created by translating a blog created in Japanese into English using the DeepL translation.

Please forgive me if some of the English text is a little strange

 

About date and time types (DateTime)

What is the DateTime type?

String type is used for string variables, Int type for integer variables, and DateTime type for date and time variables.

The DateTime type is used to store dates and times.

 

Advantages of the DateTime type

Unlike the String type, the DateTime type allows for easy date calculations and comparisons.

Example of Date Calculation
  • Calculate 3 days after the target date.
    (ex:  12/30/2020 のafter 3 days ⇒ 1/2/2021 )
  • Calculate the difference between dates.
    (ex:  11/29/2020,12/3/2020  diff ⇒ 4 days)
  • Arrange multiple dates in order
    (ex: 1/152021, 12/1/2020, 12/20/2020  Sort by ascending order ⇒ (1)12/1/2020, (2)12/20/2020 (3)1/152021  )

The actual code will be presented in a later chapter.

sea otter
sea otter
By using the DateTime and String types effectively, you can develop efficiently.

 

Create a variable of type DateTime.

Variables of the DateTime type can be set by changing the variable type to DateTime in the Variables panel.

Specifically, follow the steps below to configure the settings.

1.  Click [Variables] in the bottom row.

 

2. Click [Create Variable].

3. Enter a variable name of your choice.

4. Click the type of the variable, and then click Browse Types.

5.  Enter [System.DateTime]

6. Double-click [DateTime] in mscorlib>System

7. The variable type is now set to DateTime.

[Reference].The most recently selected type will be displayed in the list of variable types, click on it.

 

Getting the date and time

Get the current date and time (DateTime.Now )

Use DateTime.Now to get the current date and time.

 

Code to get the current date and time

DateTime.Now

 

・Sample Process

・Execution result

 

Get the current date and time separately (DateTime.Now, ToString).

To get the current date and time separately, use DateTime.Now and ToString.

 

Code to get the current date

DateTime.Now.ToString("MM/dd/yyyy")

 

・Sample Process  No.1

・Execution result  No1

 

 

Code to get the current hour, minute, and second

DateTime.Now.ToString("hh:mm:ss tt")

 

・Sample Process  No.2

 

・Execution result  No.2

 

・Example format

Format specifier Description Examples(12-6-2020(Sunday) 13:04:56.7890123)
d The day of the month, from 1 through 31. 12-6-2020(Sunday) 13:04:56
-> 12
dd The day of the month, from 01 through 31. 12-6-2020(Sunday) 13:04:56
-> 12
ddd The abbreviated name of the day of the week. 12-6-2020(Sunday) 13:04:56
-> Sun
dddd The full name of the day of the week. 12-6-2020(Sunday) 13:04:56
-> Sunday
f The tenths of a second in a date and time value. 12-6-2020(Sunday) 13:04:56.7890123
-> 7
ff The hundredths of a second in a date and time value. 12-6-2020(Sunday) 13:04:56.7890123
-> 78
fff The milliseconds in a date and time value. 12-6-2020(Sunday) 13:04:56.7890123
-> 789
h The hour, using a 12-hour clock from 1 to 12. 12-6-2020(Sunday) 13:04:56.7890123
-> 1
hh The hour, using a 12-hour clock from 01 to 12. 12-6-2020(Sunday) 13:04:56.7890123
-> 01
H The hour, using a 24-hour clock from 0 to 23. 12-6-2020(Sunday) 13:04:56.7890123
-> 13
HH The hour, using a 24-hour clock from 00 to 23. 12-6-2020(Sunday) 13:04:56.7890123
-> 13
m The minute, from 0 through 59. 12-6-2020(Sunday) 13:04:56.7890123
-> 4
mm The minute, from 00 through 59. 12-6-2020(Sunday) 13:04:56.7890123
-> 04
M The month, from 1 through 12. 12-6-2020(Sunday) 13:04:56.7890123
-> 12
MM The month, from 01 through 12. 12-6-2020(Sunday) 13:04:56.7890123
-> 12
s The second, from 0 through 59. 12-6-2020(Sunday) 13:04:56.7890123
-> 56
ss The second, from 00 through 59. 12-6-2020(Sunday) 13:04:56.7890123
-> 56
tt The AM/PM designator. 12-6-2020(Sunday) 13:04:56.7890123
-> PM
yy The year, from 00 to 99. 01-15-2021(Sunday) 13:04:56.7890123
-> 21
yyyy The year as a four-digit number. 01-15-2021(Sunday) 13:04:56.7890123
-> 2021
\ The escape character. 01-15-2021(Sunday) 13:04:56 ,h\h
->1h

 

F-pen
F-pen
For other formats, please refer to the following link.

 

・Other samples and execution results
Left side of // is code, right side of // is output result

Output result when the current date and time is [12-6-2020(Sunday) 13:04:56]
DateTime.Now.ToString("MM-dd-yyyy")    //12-06-2020
DateTime.Now.ToString("M-d-yyyy")      //12-6-2020
DateTime.Now.ToString("yyyy")       //2020
DateTime.Now.ToString("MM")        //12
DateTime.Now.ToString("dd")        //06

DateTime.Now.ToString("yyyy/MM/dd ddd")  //12-06-2020 Sun
DateTime.Now.ToString("yyyy/MM/dd dddd")  //12-06-2020 Sunday
DateTime.Now.ToString("ddd")        //Sun
DateTime.Now.ToString("dddd")        //Sunday

DateTime.Now.ToString("hh:mm:ss")    //01:04:56
DateTime.Now.ToString("HH:mm:ss")    //13:04:56
DateTime.Now.ToString("h:m:s")       //1:4:56
DateTime.Now.ToString("hh")           //01
DateTime.Now.ToString("HH")           //13
DateTime.Now.ToString("mm")           //04
DateTime.Now.ToString("ss")           //56

DateTime.Now.ToString("MM-dd-yyyy(ddd) HH:mm:ss")    //12:06:2020(Sun) 13:04:56

 

 

Calculate date and time differences

Get the difference between date and time (DateDiff)

Use DateDiff to calculate the difference in date and time.

 

Code to calculate the difference in days between date2 minus date1

DateDiff("d",date1,date2)
  • First value in parentheses : time unit used for the calculation
  • The second value in parentheses: the date used for the calculation
  • Third value in parentheses: the date used for the calculation

 

・Sample Process

* date1 and date2 are datetime type
* date1 = 12/05/2020 01:23:56
* date2 = 12/08/2020 05:34:19

 

・Execution result

 

・Setting the time unit for the first value in parentheses

Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second

 

・Other samples and execution results
Left side of // is code, right side of // is output result

date1=2018-12-05 ,date2=2019-11-08
DateDiff("yyyy",date1,date2)  //1
DateDiff("yyyy",date2,date1)  //-1
DateDiff("m",date1,date2)    //11
DateDiff("d",date1,date2)    //338
DateDiff("w",date1,date2)    //48
DateDiff("ww",date1,date2)   //48

date1=2020/12/05 14:34:6 ,date2=2020/12/05 16:13:04
DateDiff("h",date1,date2)    //1
DateDiff("n",date1,date2)    //98
DateDiff("s",date1,date2)    //5938
F-pen
F-pen
If the second value is larger than the third value in () of DateDiff, it will be printed as a minus value.

 

Add a time interval to the date and time (DateAdd)

To add a specified time interval to the date and time, use DateAdd.

 

Code to add 1 year to DataTime type date1

DateAdd("yyyy",1,date1)
  • The first value in parentheses: the time unit used for the calculation.
  • The second value in parentheses: the value to add
  • The third value in parentheses: the original date and time.

 

・Sample Process

* date1 is Datetime Type
* date1 = 12/05/2019 14:34:06

 

・Execution result

 

・Set the time unit for the first value in ().

Setting Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week
h Hour
n Minute
s Second

 

・Other samples and execution results
Left side of // is code, right side of // is output result

date1=2019/12/05 14:34:6
DateAdd("yyyy",1,date1)    //12/05/2020 14:34:06
DateAdd("yyyy",-1,date1)    //12/05/2018 14:34:06
DateAdd("q",2,date1)       //06/05/2020 14:34:06
DateAdd("m",3,date1)       //03/05/2020 14:34:06
DateAdd("y",4,date1)       //12/09/2019 14:34:06
DateAdd("d",5,date1)       //12/10/2019 14:34:06
DateAdd("w",10,date1)       //12/15/2019 14:34:06
DateAdd("ww",2,date1)       //12/19/2019 14:34:06
DateAdd("h",11,date1)       //12/06/2019 01:34:06
DateAdd("n",22,date1)       //12/05/2019 14:56:06
DateAdd("s",33,date1)       //12/05/2019 14:34:39

 

sea otter
sea otter
If the second value in () of DateDiff is negative, the negative time unit will be subtracted from the original date and time.

 

Get the beginning or end of the month with a specified date.

Get the first date of the month for a given day (DateTime)

To get the first date of the month for a given day, use the DateTime type.

 

Code to get the first day of the month for Date type date1

new DateTime(date1.Year,date1.Month,1)
  • The first value in DateTime(): the year to be calculated
  • The second value in DateTime(): the month to calculate
  • Third value in DateTime(): 1 (fixed to 1 since it is the beginning of the month)

 

・Sample Process

*date1 and date2 are Datetime type
*The second activity is、date2 = new DateTime(date1.Year,date1.Month,1)

 

・Execution result

 

 

Get the date at the end of the month for the specified date (DateTime)

To get the date at the end of the month for a given day, use the DateTime type.

 

Code to get the end of the month for DateTime type date1

new DateTime(date1.Year,date1.Month,1).AddMonths(1).AddDays(-1)
  • The first value in DateTime(): the year to be calculated
  • The second value in DateTime(): the month to calculate
  • Third value in DateTime(): 1 (fixed to 1 since it is the beginning of the month)
F-pen
F-pen
We calculate the end of the month by adding one month to the first day of the specified date and subtracting one day

 

・Sample Process

*The second activity is、date2 = new DateTime(date1.Year,date1.Month,1).AddMonths(1).AddDays(-1)

 

・Execution result

 

Conversion between Western and Japanese calendars

Conversion from Western to Japanese calendar (CultureInfo, ToString)

To convert from Western to Japanese calendar year, use the CultureInfo type and ToString.

 

Code to get the Japanese calendar for date1 of type DateTime

date1 = 2020-12-05
CultureJP = New CultureInfo("ja-JP")
CultureJP.DateTimeFormat.Calendar = New JapaneseCalendar
date1.ToString("ggy年M月d日",CultureJP)
sea otter
sea otter
The second and third lines set the format for the Japanese calendar, and the third line uses the set format to get the Japanese calendar.

 

・Sample Process

* The second activity is
CultureJP = New CultureInfo(“ja-JP”)
* The third activity is
CultureJP.DateTimeFormat.Calendar = New JapaneseCalendar

・Setting variables

* Search Word [System.Globalization.calendar]

 

・Execution result

・Other samples and execution results
Left side of // is code, right side of // is output result

date1 = 2020-12-05
CultureJP = New CultureInfo("ja-JP")
CultureJP.DateTimeFormat.Calendar = New JapaneseCalendar
date1.ToString("ggy年M月d日",CultureJP)        //令和2年12月5日
date1.ToString("ggyy年MM月dd日",CultureJP)     //令和02年12月05日

 

 

Convert from Japanese year to Western year (CultureInfo, ToString)

To convert from Japanese calendar year to Western calendar year, use the CultureInfo type and ToString.

 

Code to convert the Japanese calendar of String type str1 to the Western calendar

str1 = "令和2年12月5日"
date1 = Date.Parse(str1, New CultureInfo("ja-JP"))
date1.Tostring("yyyy年MM月dd日")
F-pen
F-pen
The second line sets the Japanese calendar of type String to type DateTime, and the third line outputs the string of the Western calendar.

 

・Sample Process

※The second activity is
date1 = Date.Parse(str1, New CultureInfo(“ja-JP”))

 

・Execution result

・Other samples and execution results
Left side of // is code, right side of // is output result

str1 = "令和2年12月5日"
date1 = Date.Parse(str1, New CultureInfo("ja-JP"))
date1.Tostring("yyyy年MM月dd日")   //2020年12月05日

str1 = "令2年12月5日"
date1 = Date.Parse(str1, New CultureInfo("ja-JP"))
date1.Tostring("yyyy年MM月dd日")   //2020年12月05日

str1 = "R2年12月5日"
date1 = Date.Parse(str1, New CultureInfo("ja-JP"))
date1.Tostring("yyyy年MM月dd日")   //2020年12月05日

str1 = "平成2年12月5日"
date1 = Date.Parse(str1, New CultureInfo("ja-JP"))
date1.Tostring("yyyy年M月d日")   //1990年12月5日

str1 = "平2年12月5日"
date1 = Date.Parse(str1, New CultureInfo("ja-JP"))
date1.Tostring("yyyy年M月d日")   //1990年12月5日

str1 = "H2年12月5日"
date1 = Date.Parse(str1, New CultureInfo("ja-JP"))
date1.Tostring("yyyy年M月d日")   //1990年12月5日
sea otter
sea otter
For the Japanese calendar, you can use “令和,昭和,大正,明治,令,昭,大,明,R,H,S,T,M”

 

Checking the DateTime type

Check if it can be recognized as a date or time (IsDate)

Checking if a string can be recognized as a date or time is done using IsDate.

 

Code to check if str1 of type String can be recognized as a date or time

IsDate(str1)
  • First value in (): string to be checked

 

・Sample Process

 

・Execution result

・Other samples and execution results
Left side of // is code, right side of // is output result

IsDate("2019/12/5")        //True
IsDate("2019/20/20")       //False
IsDate("05/12/2020")       //True
IsDate("5/1")           //True
IsDate("5/99")          //False
IsDate("05.12.19")         //True
IsDate("05.12.2019")       //True
IsDate("December05, 2019")   //True
IsDate("abcde")            //False
IsDate("12345")            //False
IsDate("12:45")            //True
IsDate("12:45:54")         //True
IsDate("12:45:99")         //False
IsDate("2019/12/5 12:45")    //True
IsDate("2019/12/5 12:45:54")  //True

 

Converting between DateTime and String types

Converting String type date to DateTime type (DateValue)

DateValue is used to convert a year, month, and day string to DateTime type.

 

Code to convert a String type str1 with a date assigned to it to a DateTime type

str1 = "2020/12/5"
DateValue((str1))
  • First argument in DateValue(): String to be converted to Date type

 

・Sample Process

 

・Execution result

sea otter
sea otter
The time will be converted to zero.

 

・Other samples and execution results
Left side of // is code, right side of // is output result and description

str1 = "2020/12/5"
DateValue((str1))     //Converted to "12/05/2020 00:00:00" datetime type 

str1 = "2020/12/05"
DateValue((str1))     // Converted to "12/05/2020 00:00:00" datetime type

str1 = "2020年12月5日"
DateValue((str1))     // Converted to "12/05/2020 00:00:00" datetime type

str1 = "12/5"
DateValue((str1))     // If the year you run is 2020、Converted to "12/05/2020 00:00:00" datetime type

str1 = "2020/99/99"
DateValue((str1))     // Execution error due to non-existent date.

 

 

Convert String type time to DateTime type (TimeValue)

String time can be converted to DateTime by using the TimeValue function.

Code to convert a String type str1 with time assigned to it to a DateTime type

str1 = "13:04:22"
TimeValue((str1))
  • First argument in TimeValue(): string of time to be converted to DateTime type

 

・Sample Process

・Execution result

F-pen
F-pen
Note that the year, month, and day is 1, not the present.

 

・Other samples and execution results
Left side of // is code, right side of // is output result and description

str1 = "13:04:22"
TimeValue(str1)     // Converted to "01/01/0001 13:04:22" datetime type 

str1 = "13時04分22秒"
TimeValue(str1)     // Converted to "01/01/0001 13:04:22" datetime type

str1 = "1:04:22 PM"
TimeValue(str1)     // Converted to "01/01/0001 13:04:22" datetime type

str1 = "13:04"
TimeValue(str1)     // Converted to "01/01/0001 13:04:22" datetime type

str1 = "13"
TimeValue(str1)     // Execution error.

 

 

 

Convert String type date and time to DateTime type (CDate)

To convert String type date and time to DateTime type, use the CDate function.

Code to convert a String type str1 with date and time assigned to it to a DateTime type

str1 = "2020/12/5 13:04:22"
CDate(str1)
  • First argument in CDate(): string of the date and time to be converted to DateTime type

 

・Sample Process

 

・Execution result

 

・Other samples and execution results
Left side of // is code, right side of // is output result and description

str1 = "2020/12/5 13:04:22"
CDate(str1)       //  Converted to "12/05/2020 13:04:22" datetime type 

str1 = "2020年12月5日 13時04分22秒"
CDate(str1)       // Converted to "12/05/2020 13:04:22" datetime type

str1 = "2020/12/5 99:88:77"
CDate(str1)       // An error occurs because the time does not exist.

 

 

Converting DataTime type to String type (ToString)

To convert Data type to String type, use the ToString function.

 

Code to convert a DateTime type date1 variable to a String type

date1 = 2020-12-05
date1.ToString

 

・Sample Process

・Execution result

 

Summary

  • Using the DateTime type, date and time can be easily calculated.
  • The current date and time is obtained using DateTime.
  • Date and time can be calculated using DateDiff, DateAdd, DateTime, ToString functions and CultureInfo type.
  • Use IsDate to check the date and time.
  • To convert between date and time and string, use DateValue, TimeValue, and CDate

 

Back to Table of Contents

 

 Related Articles Create a practical UiPath robot with Udemy’s online course

 同カテゴリ UiPath