How do I convert UTC to CST in c# Expression

Hello,

Is there a good way to convert a date that is UTC to my local time (CST) that will account for DST?

It needs to be done in a ‘Set Argument/Variable’ in a BPM.

Any help is appreciated!

See if this helps:
Converting times between time zones - .NET | Microsoft Learn

I have tried it a few different ways and I keep getting “cannot convert from ‘System.DateTime?’ to 'System.DateTime”.

Any ideas how to get around that?

Can you provide more information on where that error is occuring?
or use my best guess
Assumption try something like:

DataTime myCstDateTime = (DateTime)(timeUtc==null?DateTime.Now:TimeZoneInfo.ConvertTimeFromUtc(timeUtc, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")));

System.DateTime? is a nullable date. System.DateTime is a non-nullable.

To get the value out of System.DateTime? you need .value

YouDateField.Value

I am trying to use a Set Variable in a BPM to be a Date.

The Variable is DateTime:
image

My Original Expression which gives the UTC time is:

queryResultDatasetResultsRow.SysTask_StartedOn

I have tried:

TimeZone.ToLocalTime(queryResultDatasetResultsRow.SysTask_StartedOn)

And the error I get is:

CS1503 Argument 1: cannot convert from ‘System.DateTime?’ to ‘System.DateTime’

Try:

this.StartedOn = (DateTime)(queryResultDatasetResultsRow.SysTask_StartedOn==null?DateTime.Now:TimeZoneInfo.ConvertTimeFromUtc(queryResultDatasetResultsRow.SysTask_StartedOn, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")));

When you put that in and click ‘Check Syntax’, you get this:

BPM003 Expression should contain only one statement

When I trim it down to this:

(DateTime)(queryResultDatasetResultsRow.SysTask_StartedOn==null?DateTime.Now:TimeZoneInfo.ConvertTimeFromUtc(queryResultDatasetResultsRow.SysTask_StartedOn, TimeZoneInfo.FindSystemTimeZoneById(“Central Standard Time”)))

I get the same error as before:

CS1503 Argument 1: cannot convert from ‘System.DateTime?’ to ‘System.DateTime’

Try this instead:

TimeZoneInfo.ConvertTimeFromUtc(queryResultDatasetResultsRow.SysTask_StartedOn, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")) ?? DateTime.Now

Nope same converting error.

There has got to be a way to do this!

Did you try adding .value to this? (I’m not sure if its V or v, but use ctrl+space to see the autocomplete.)

queryResultDatasetResultsRow.SysTask_StartedOn.value

1 Like

It’s complaining about the ConvertTimeFromUtc getting a nullable item. If this doesn’t work try combining with @Banderson suggestion:

(queryResultDatasetResultsRow.SysTask_StartedOnUtc==null?DateTime.Now:TimeZoneInfo.ConvertTimeFromUtc(queryResultDatasetResultsRow.SysTask_StartedOnUtc??DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time")))

Holy Crap!

I got it to work using both of your ideas!

The code that finally did it was:

TimeZoneInfo.ConvertTimeFromUtc(queryResultDatasetResultsRow.SysTask_StartedOn.Value, TimeZoneInfo.FindSystemTimeZoneById(“Central Standard Time”))

I took the code @CSmith did in Post 9 and changed by removing the ?? DateTime.Now and then adding the .Value that @Banderson suggested.

It passed the syntax check.

I triggered the BPM and the date and time were spot on!

I am truly grateful for both of you taking the time to puzzle through this.

Hopefully this answer will help other people in the future.

3 Likes

It appears @Banderson gave you the proper solution then it was adding the “.Value” to keep it from complaining about the nullable DateTime? type by using .Value it should at minimum get an empty string which will throw a runtime exception if it encounters one.

I am not worried about it since every record created will have a date attached.

Thank you again for the help. I never would have reached the finish line without it!

Welcome!