DateTime Operations in UiPath

DateTime Formats and Conversions in UiPath

In this article you will find all that you need to work with DateTime Formats and conversions.

Formats:

Hour (12-hour clock) -> h ex: 6, hh ex:06

Hour (24-hour clock)-> Must be capital always. H ex:13, HH ex:20

Minutes -> m ex: 2, mm ex: 02

Seconds -> s ex: 2, ss ex:03

Time->This is for AM and PM.  t ex: P, tt ex: PM

Day in number-> d ex: 6 from 1-31 day of month, dd ex: 22 from 01 -31 day of month

Day in string ->ddd ex: Sun, dddd ex: Sunday

Months->Must be capital always. M ex: 7, MM ex: 07, MMM ex: Jul, MMMM ex: July

Year->y or yy ex:20, yyy or yyyy ex:2020

Time Zone -> K ex: +04:00, z, zz, zzz ex: +4, +04, +04:00

Examples for different formats in UiPath

Example 1:

Input:

Output:

Example 2:

Input:

Output:

Example 3:

Input: Current system date

Output:

Example 4:

Input: Current system date

Output:

Example 5:

Input: Current system DateTime with Time zone

Output:

Example 6:

Input: System DateTime which displays AM or PM

Output:

DateTime Properties:

There are several properties like Day, Month, Year, Hour, Minute, Second, DayOfWeek and others in a DateTime object which can be retrieved.

Example1:

Input:

Output:

Adding DateTime:

Below are the methods available in UiPath for adding DateTime:

Example1:

Input:

inputDate=”4/22/2020 3:30:20”

Convert.ToDateTime(inputDate).AddMonths(3).ToString(“MM”)

Output:

Subtracting two Dates:

Example1:

Input:

inputDate = “10/22/2020 3:30:20” or Convert.ToDateTime(“10/22/2020 3:30:20”).ToString(“MM”)

CurrentDate= Now.ToString or Convert.ToDateTime(Now).ToString(“MM”)

Output:

convert.ToDateTime(inputDate).Month-convert.ToDateTime(Now).Month

or

Convert.ToInt32(inputDate)-Convert.ToInt32(currentDate)

Example2:

Input:

inputDate and currentDate of type System.DateTime

Output:

Convert DateTime to specified type:

Methods available are ToBinary, ToOADate, ToLongDateString, ToShortDateString, ToLongTimeString, ToShortTimeString, ToString, ToUniversalTime, ToFileTime, ToLocalTime

Example 1:

Input:

inputDate =Convert.ToDateTime(“10/22/2020 3:30:20”).ToShortDateString

Output:

Example2:

Input:

inputDate = Convert.ToDateTime(“10/22/2020 3:30:20”).ToShortTimeString

Output:

String to DateTime Conversion in C#

There are two methods available:

Depending on your requirement you can

DateTime.Parse: This method is used to convert string date time representation to DateTime object.

Value: It is string representation of date and time.

Provider: It is an object which provides culture specific info.

For example: System.Globalization.CurrentInfo.CurrentCulture

Styles: It defines the formatting options that customize string parsing for some date and time parsing methods.

DateTime.Parse(String value, IFormatProvider provider, DateTypeStyles styles)

Example1:

Input:

Output:

DateTime.ParseExact: This method can be used when you want to get exact format of date. There are different arguments which you need to pass while converting to a format.

Value: It is string representation of date and time.

Formats: It is format specifier that defines a date look like after conversion. For example: “dd/MM/yyyy”

Provider: It is an object which specify culture info.

For example: System.Globalization.CurrentInfo.CurrentCulture

Style: It defines the formatting options that customize string parsing for some date and time parsing methods.

DateTime.ParseExact(string value, string[] formats, IFormatProvider provider, DateTimeStyles style)

Example 1:

Input:

Consider your Input: string in below format:

Output:

Timespan  in C#:

Example1:

Input:

inputDate and currentDate type as DateTime and timeSpan type as TimeSpan

Output:

Example2:

TimeSpan class provides FromDays, FromHours, FromMinutes, FromSeconds, and FromMilliseconds to create TimeSpan objects from days, hours, minutes, seconds, and milliseconds and add, subtract, negate methods to add, subtract, negate methods

Input:

Output:

We hope you enjoyed the article!!

You can connect with Kishori Afzulpurkar, she is always happy to help. 🙂

Thank you!!

Leave a Reply