How to fix Date Time data type issue in Blue Prism?

. 4 mins read

How to fix Date Time data type issue in Blue Prism?

Wait, what? Is there an issue with Date Time data type in Blue Prism?

Unfortunately, yes. Blue Prism converts Date Time datatype returned by code stages to UTC without any consideration about user’s intention.

The Problem

In any automation project, we must deal with dates. We might have to manipulate dates, get dates from SharePoint etc. For some of these activities, we make use of code stage in Blue Prism.

To explain the problem of date time data type in Blue Prism, let us consider a scenario where we want to return a date time data type using a date time constructor.

Let us use the date time constructor as new DateTime(2012, 12, 25, 10, 30, 50);.

This should return a date-time data type with value 25 December 2012 10:30:50.

Now, let us use this in a Blue Prism code stage.

Blue Prism Code Stage

Input

For this example, we have a hardcoded date time constructor represented as new DateTime(2012, 12, 25, 10, 30, 50);.

Outputs

NameData TypeStore In
dateFromCodeStringTextString Representation of Date from Code
bpDateDateTimeBlue Prism Date Time Object

The Code

DateTime dateFromCode = new DateTime(2012, 12, 25, 10, 30, 50);
dateFromCodeString = dateFromCode.ToString("dd-MM-yyyy HH:mm:ss");
bpDate = dateFromCode;

Analyzing Output

NameExpected ValueActual Value
String Representation of Date from Code25-12-2012 10:30:5025-12-2012 10:30:50
Blue Prism Date Time Object25-12-2012 10:30:5025-12-2012 05:00:50

The Difference

For the above example, we ran Blue Prism on a machine with the time zone configured as Indian Standard Time (IST). IST is 5 hours and 30 minutes ahead of UTC.

We can observe the same difference between the expected date time value and the actual date time value returned by Blue Prism.

Actual Value = Expected Value - 5 hours 30 minutes
25-12-2012 05:00:50 = (25-12-2012 10:30:50 - 05:30) = 25-12-2012 05:00:50

Problem Conclusion

From the example above, we can conclude that Blue Prism converts the date time data type returned from a code stage to UTC. Blue Prism does this conversion based on the local time zone of the machine in which it is running.

How to fix the problem?

To fix this problem, we need to understand what Blue Prism does to the actual date time object returned from the code stage.

If we take the above example, the local machine time zone is IST. IST has an offset of 5 hours and 30 minutes from UTC.

So, Blue Prism subtracted the offset from the date time returned by the code stage.

To fix this, we must add the offset back to the date returned from code stage. We can do this addition within the code stage itself.

In a way we are giving Blue Prism a date time with an extra offset added, so that when it converts to UTC, we get the desired date time.

Steps to fix

  • Find the UTC offset of the local time.
var utcOffset = System.TimeZoneInfo.Local.GetUtcOffset(dateFromCode);
  • Add the UTC offset to the date from code.
bpDate = dateFromCode.Add(utcOffset);

The final Code

DateTime dateFromCode = new DateTime(2012, 12, 25, 10, 30, 50);
dateFromCodeString = dateFromCode.ToString("dd-MM-yyyy HH:mm:ss");
var utcOffset = System.TimeZoneInfo.Local.GetUtcOffset(dateFromCode);
bpDate = dateFromCode.Add(utcOffset);

Analyzing the fixed Output

NameExpected ValueActual Value
String Representation of Date from Code25-12-2012 10:30:5025-12-2012 10:30:50
Blue Prism Date Time Object25-12-2012 10:30:5025-12-2012 10:30:50

Alternate Approach

If you know the time zone settings of the Blue Prism bot machine and you are sure that nobody will change the settings, you can add the timezone difference to the date time returned by Blue Prism to get the actual time. However, this is not a recommended method.

DateTime dateFromCode = new DateTime(2012, 12, 25, 10, 30, 50);
dateFromCodeString = dateFromCode.ToString("dd-MM-yyyy HH:mm:ss");
var timeSpanAdjustment = new System.TimeSpan(5, 30, 0);
bpDate = dateFromCode.Add(timeSpanAdjustment);

If your timezone is UTC - 3 use adjustment as new System.TimeSpan(-3, 0, 0);.

If your timezone is UTC - 4:30 use adjustment as new System.TimeSpan(-4, 30, 0);.

RPA
Blue Prism Video Tutorial
03 Jul 2017

A well-structured video tutorial on Blue Prism, which could be the right starting point for your RPA career.