Understanding DateTime Variables:
UiPath uses the Date Time data type to represent dates and times. This versatile data type allows you to perform various operations on dates and times, making it a crucial element in automation workflows.
![](https://static.wixstatic.com/media/11062b_4910111ce6c44ed18445d1cfa38e7da8~mv2.jpg/v1/fill/w_147,h_105,al_c,q_80,usm_0.66_1.00_0.01,blur_2,enc_auto/11062b_4910111ce6c44ed18445d1cfa38e7da8~mv2.jpg)
Â
1.  Get Current Date in string format
DateTime.Now.ToString() (Output DataType:Â System.String) - Get Current Date in string format. Output Format: MM/dd/yyyy HH:mm:ss
DateTime.Now (Output DataType:Â System.DateTime) - Get current DateTime in DateTime variable.
DateTime.Now.Day (Output DataType: System.Int32) - Get Date
DateTime.Now.DayOfWeek (Output DataType:Â System.DayOfWeek) - Get Day of week.
DateTime.Now.DayOfYear (Output DataType:Â System.Int32) - Get count of day of year.
Â
2. Get different formats of datetime as string type
DateTime.Now.ToString("d")Â - Prints whole Date
Datetime.Now.ToString("dd") - Get current day alone - numerical terms
Datetime.Now.ToString("dddd") - Get current day - full text
Datetime.Now.ToString("MMMM") - Get Current month - full name
Datetime.Now.ToString("MMM") - Get current month - first three characters
Datetime.Now.ToString("MM") - Get current month - numerical value
Datetime.Now.ToString("yyyy") - Get current year - full year
Datetime.Now.ToString("yy") - Get current year - last two characters
Datetime.Now.ToString("h:mm:ss") - Get current time - in 12 hours format
Datetime.Now.ToString("H:mm:ss") - Get current time - in 24 hours format
Datetime.Now.ToString("hh") - Get current hour alone - 12 hours format
Datetime.Now.ToString("HH") - Get current hours alone - 24 hours format
Datetime.Now.ToString("mm") - Get Current minutes
Datetime.Now.ToString("ss") - Get current seconds
DateTime.Now.ToString("h:mm") - Get hours and minutes in 12 hours format
DateTime.Now.ToString("H:mm") - Get hours and minutes in 24 hours format
Â
3. Get current Datetime as string with TIMEZONE
Datetime.Now.ToString("dd/MM/yyyy HH:mm:ss K")
(Output format:Â dd/MM/yyyy HH:mm:ss +5:30)
Â
Â
4. Get current Datetime with AM / PM
Datetime.Now.ToString("dd/MM/yyyy HH:mm:ss tt")
(Output format:Â dd/MM/yyyy HH:mm:ss AM/PM)
Â
5. Convert a string to Datetime
If Input:Â str_Input1 = "02/10/2024"
with Convert.ToDatetime method
         var_DateTime1 = CDate(str_Input1)
with Datetime.Parse method
         var_DateTime2 = Datetime.Parse(str_Input1.ToString)
with Datetime.ParseExact method
         var_DateTime3 = Datetime.ParseExact(str_Input1.ToString, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
(Output DataType:Â System.DateTime)
with Datetime.ParseExact method
         var_DateTime4 = Datetime.ParseExact(str_Input1.ToString, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString("MM/dd/yyyy")
(Output DataType: System.String)
Â
If Input:Â str_Input2 = "01/19/2024 06:06:30 +5:30"
             var_DateTime5 = Datetime.ParseExact(str_Input2.ToString, "MM/dd/yyyy hh:mm:ss K", System.Globalization.CultureInfo.InvariantCulture)Â
(Output DataType:Â System.DateTime)
Â
Difference between Parse and ParseExact Method:
Parse:
This method is used to convert a string to datetime object using default date and time format of system current culture. It can handle date time formats of current culture settings. If input string cannot be parsed into valid datetime it will throw an exception.
Â
ParseExact:
This method is used to convert string of date and time into datetime object on specific date and time format. This input string must match with the exact format specified in the format string parameter. This method is useful when you know the exact format of input string.
Â
6. Validate whether a Datetime is valid date
If Input:Â str_Input1 = "08/12/2023"
              bool_date = DateTime.TryParseExact(str_Input1.ToString,"MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, Nothing)Â
(Output DataType: System.Boolean)
Â
TryParseExact:
This gives Boolean output and validates whether the string input matches with the format mentioned in format string parameter.
Â
7. Convert a String of multiple possible date formats to a Datetime variable
Say you have a date string which can be of different formats for each time it is being used like "dd/MM/yyyy", "MM/dd/yyyy", "dd/yy". You can store the different date formats in an array variable named arr_formats.
Â
If Input:Â str_Input1= "02/10/2024"
             arr_Input = {"dd/MM/yyyy", "MM/dd/yyyy", "dd/yy"}
             var_DateTime = DateTime.ParseExact(str_Input1.ToString, arr_Input, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None)
(Output DataType: System.DateTime)
Â
8. Convert a string to Datetime of specific culture info
If Input:Â strInput = "08-11-2023"
              str_datetime = Datetime.ParseExact(Strinput.ToString, "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString("dd/MM/yyyy", New CultureInfo("de-DE"))
(Output DataType: System.String, Output Format: dd.MM.yyyy)
Â
Conclusion:
In this blog post, we've covered the basics of date and time manipulation in UiPath, providing you with a solid foundation to start incorporating date and time functionality into your automation projects. We've explored essential concepts such as parsing dates from strings, formatting dates into different representations, and performing common operations. However, it's important to note that date and time manipulation is just the beginning of what you can achieve with UiPath. There are still many more concepts and advanced techniques to explore, such as working with time zones, handling date ranges, and integrating with external calendars or scheduling systems.
Â
Keep exploring and enjoy the journey ahead! With every automation, you're not just solving problems – you're expanding your skills and knowledge. So, stay curious, keep learning, and happy automating!!
![](https://static.wixstatic.com/media/9d7901_c2b366fe04384b66bd723006b8f65ad0~mv2.jpg/v1/fill/w_100,h_100,al_c,q_80,usm_0.66_1.00_0.01,blur_2,enc_auto/9d7901_c2b366fe04384b66bd723006b8f65ad0~mv2.jpg)
PS Parvathy Associate Software Engineer - RPA
XTGlobal, Inc.