top of page

Basics of Date Time Manipulation in UiPath

Writer's picture: PS ParvathyPS Parvathy

Updated: Apr 11, 2024

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.




 

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("dd") - Get current day alone - numerical terms



  • 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("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!!



XTGlobal, Inc.





468 views0 comments

Recent Posts

See All
Post: Blog2_Post
bottom of page