How to convert DateTime variable to String in UiPath

Converting a DateTime variable to a string is the most important part of Automation.

How do we get the system date and time in UiPath?

System.DateTime.Now

By using the above syntax you will be able to get the date and time at that particular time.

How we convert this to string?

We need to add this at the end .ToString as shown below:

System.DateTime.Now.ToString

As of now, we converted a DateTime Variable to String. We now see how we can convert this into a different format.

What is the common DateTime format?

How we can change it to different format?

System.DateTime.Now.ToString(“dd-MM-yyyy”)

This will give us the date format as shown below

   Changed Date Format : 02-11-2020
datetime to string

While automating different business processes may demand different time formats.

How we can make different datetime formats in UiPath?

You have to know how to differentiate between day, month, year, hour, minute, seconds by following the below letters.

  • d -: Represents the day of the month i.e 1,2,16 or 31
  • dd -: Represents the day of the month i.e 01,05,14 or 31
  • ddd -: Represents the abbreviated name of the day i.e Mon, Tue or Wed
  • dddd -: Represents the full name of the day i.e Monday, Tuesday or, Wednesday
  • M -: Represents the month number i.e 1,5 or 12
  • MM -: Represents the month number with leading zero i.e 04,09 or 12
  • MMM -: Represents the abbreviated month Name i.e Jan, May, or Dec
  • MMMM -: Represents the full month name i.e. January, June or, December
  • y -: Represents the year i.e. 2019 as 19
  • yy -: Represents the year with a leading zero i.e. 2019 as 019
  • yyy -: Represents the year i.e 2019
  • yyyy -: Represents the year i.e 2019
  • h -: Represents the 12-hour clock i.e 4,11 or 2
  • hh -: Represents the 12-hour clock with a leading 0 i.e. 04,05 or 12
  • H -: Represents the 24-hour clock i.e 13,18 or 22
  • HH -: Represents the 24-hour clock with a leading 0 i.e. 04,09 or 22
  • m -: Represents the minutes i.e. 12,40 or 56
  • mm -: Represents the minutes with a leading zero i.e 04,09 or 23
  • s -: Represents the seconds i.e 9,35 or 40
  • ss -: Represents the seconds with leading zero i.e. 04,09 or 35

By using these patterns we can easily get the DateTime in the required format.

That’s it for this post on DateTime Operations in UiPath.

Happy Learning!

Like this post then let your friends know about this-:

4 thoughts on “How to convert DateTime variable to String in UiPath

    • Author gravatar

      I have to convert 23rd May 2021 to 5/23/2021 in UiPath. How to do this pattern

      • Author gravatar

        Hi Shabhaz, I am assuming your input as string then follow the below code to get the required output – Convert.ToDateTime(“23 May 2021”).ToString(“MM/dd/yyyy”)

        • Author gravatar

          Hi ADITYA, great guide on date formating but could you tell me if it is possible to change from the normal date format say: 03/05/2022 to the one that the first comenter mentioned which is 3rd of May 2022 (emphasis on adding the rd or th at the end of the date) i’d like to know if this is possible since i would like to use it the body of an outlook email activity. Could you share how to do it?

          • Author gravatar

            Hi Gabriel, I am not sure of any direct method that is available but you can write a switch case based on the day value and add the corresponding(st, nd, th) to the date you have.

Leave a Reply

Your email address will not be published. Required fields are marked *