Basics of Date Time Manipulation in UiPath
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.
- 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
- 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")
4. Get current Datetime with AM / PM
- Datetime.Now.ToString("dd/MM/yyyy HH:mm:ss tt")
5. Convert a string to Datetime
If Input: str_Input1 = "02/10/2024"
- with Convert.ToDatetime method
- with Datetime.Parse method
- with Datetime.ParseExact method (Output DataType: System.DateTime)
var_DateTime3 = Datetime.ParseExact(str_Input1.ToString, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture)
- with Datetime.ParseExact method (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.
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)
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!!
PS Parvathy
Comments
Post a Comment