top of page

Advanced Date Time Manipulation in UiPath

Writer's picture: PS ParvathyPS Parvathy

Welcome back to our DateTime Manipulation series! In this second part, we'll delve into advanced techniques that build upon the basics covered in our previous post. Get ready to explore topics like handling time zones and mastering complex date calculations as we take your UiPath skills to the next level. Let's dive in!





1. Difference in days between two dates

        If Input: Strinput = "10.01.2023"


int_Days = DateDiff(DateInterval.Day, DateTime.ParseExact(Strinput.ToString, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture),DateTime.Now)   


(Output DataType: System.Int32)


2. Difference in months between two dates

        If Input: Strinput = "08-11-2023"


int_Months = DateDiff(DateInterval.Month, DateTime.ParseExact(Strinput.ToString, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture),DateTime.Now


(Output DataType: System.Int64)


3. Difference in years between two dates

         If Input: Strinput = "08-11-2023"


int_Years = DateDiff(DateInterval.Year, DateTime.ParseExact(Strinput.ToString, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture),DateTime.Now


(Output DataType: System.Int64)

 

4. Get First date and Last date of the Current Month

str_firstdate = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1) 


(Output DataType: System.DateTime)


str_lastedate = New DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month))     


(Output DataType: System.DateTime)

 

5. Get First date and Last date of Previous Month

str_firstdate = New DateTime(now.Year,now.Month,1).AddMonths(-1)  


(Output DataType: System.DateTime)


str_lastdate = New DateTime(now.Year,now.Month,1).AddDays(-1)     


(Output DataType: System.DateTime)

 

6. Get First date and Last date of the Year

str_firstdate = New DateTime(now.Year,1,1)     


(Output DataType: System.DateTime)


str_lastdate = New DateTime(now.Year,12,31)


(Output DataType: System.DateTime)

 

7. Add hours to Current Time

str_datetime = Datetime.Now.AddHours(1)       


(Output DataType: System.DateTime)

 

8. Add days to Current Day

str_datetime = Datetime.Now.AddDays(5)         


(Output DataType: System.DateTime)

 

9. Add Months to Current Month

str_datetime3 = DateTime.Now.AddMonths(2)   


(Output DataType: System.DateTime)

 

10. Add Years to Current Year

str_datetime = Datetime.Now.AddYears(10)       


(Output DataType: System.DateTime)

 

11. Subtract days from Current Day

str_datetime = Datetime.Now.AddDays(-10)       


(Output DataType: System.DateTime)

 

12. Subtract Months from Current Month

str_datetime = Datetime.Now.AddMonths(-5)     


(Output DataType: System.DateTime)

 

13. Subtract Year from Current Year

str_datetime = Datetime.Now.AddYears(-10)       


(Output DataType: System.DateTime)

 

14. Convert a OADate in excel to a Datetime or string Datetime

Default OA date is December 30, 1899. Suppose if the actual value being entered: ‘44965' but the required value is '02/08/2023' then the format should be given as 'MM/dd/yyyy'.


        If Input: Strinput = "44965"

       

str_datetime = DateTime.FromOADate(CDbl(Strinput.ToString)).ToString("MM/dd/yyyy")  


(Output DataType: System.String)

 

15. Different string formats from a Datetime

        If Input: str_Input1 = "07/22/2023 20:30:00"

 var_DateTime = Datetime.ParseExact(str_Input1.ToString, "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture)   


(Output DataType: System.DateTime)


  • ".ToOADate" Method: ".ToOADate" is a method used to convert a DateTime object to an OLE Automation Date (OADate). OADate is a double-precision floating-point number that represents a date and time as the number of days before or after the base date.

          CDbl(var_DateTime.ToOADate).ToString


  • ".ToLongDateString" Method: ".ToLongDateString" method is used to format a DateTime object into a long date string representation. It returns the DateTime value formatted as a string using the long date pattern of the current culture set on the system. The long date pattern typically includes the full name of the day of the week, the full name of the month, and the day of the month in a specific order, depending on the culture settings. For example, in the US English culture, the long date pattern could be something like: Monday, July 22, 2023.

          var_DateTime.ToLongDateString


  • ".ToShortDateString" Method: ".ToShortDateString" method is used to format a DateTime object into a short date string representation. It returns the DateTime value formatted as a string using the short date pattern of the current culture set on the system. The short date pattern typically includes only the numeric representation of the date, without the day of the week or the full name of the month: 7/22/2023.

          var_DateTime.ToShortDateString


  • ".ToLongTimeString" Method: ".ToLongTimeString" method is used to format a DateTime object into a long time string representation. It returns the DateTime value formatted as a string using the long time pattern of the current culture set on the system. The long time pattern typically includes the hours, minutes, seconds, and possibly milliseconds, formatted in a specific order: 1:30:45 PM.


          var_DateTime.ToLongTimeString


  • ".ToShortTimeString()" Method: ".ToShortTimeString()" method is used to format a DateTime object into a short time string representation. It returns the DateTime value formatted as a string using the short time pattern of the current culture set on the system. The short time pattern typically includes the hours and minutes, formatted in a specific order: 1:30.


          var_DateTime.ToShortTimeString


  • ".ToString()" Method: ".ToString()" method is used to convert various types of data, including DateTime objects, into a string representation. When applied to a DateTime variable, it returns the DateTime value formatted as a string.


          var_DateTime.ToString


  • ".ToUniversalTime()" Method: ".ToUniversalTime()" method is used to convert a DateTime value from the local time zone to Coordinated Universal Time (UTC). UTC is a time standard that is the same worldwide, and it doesn't have time zone offsets like local time does. Converting to UTC is useful for standardizing date and time values, especially when dealing with international or distributed systems.


          var_DateTime.ToUniversalTime.ToString'


  • ".ToFileTime()" Method: ".ToFileTime()" method is used to convert a DateTime value into a 64-bit integer representing the date and time in a file time format. File time is a Windows-specific representation of time used by the file system to store date and time information for files and directories.


          var_DateTime.ToFileTime.ToString


  • ".ToLocalTime()" Method: ".ToLocalTime()" method is used to convert a DateTime value from Coordinated Universal Time (UTC) to the local time zone of the computer where the code is running. UTC is a standardized time representation used worldwide, but local time zones have different offsets based on geographical location and daylight saving time.


          var_DateTime.ToLocalTime.ToString


Conclusion

As we finish up our discussion on converting dates, we've learned how to handle different date formats effectively. Now, we're moving on to explore time conversions in our next blog. Join us as we dive into understanding how to change and work with time data. We'll keep it straightforward and easy to follow as we unravel more about dealing with time in our analyses.

 

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 automation!




PS Parvathy

XTGlobal, Inc.



287 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page