0

I am currently building an external web application using MVC. I need to format the DateTime to match Salesforce's format so I can push it up via API endpoints.

The DateTime picker in my View works just fine, it outputs the selected time in which the user inputs, then I am using a .toString("s") to format it into an acceptable form for the API.

BUT

after it gets converted, the variable I assign it to does not display the time selected, it just displays 00 (Keep in mind I'm talking in breakpoint speed).

It then gets pushed to Salesforce and the time is no where near what the user inputs.

For Example

the User would input 3:30 PM (The Date part works just fine, just the time part doesn't work) and when it gets pushed up to Salesforce, it would be 10:30AM .

Any ideas ?

1
  • Might be a timezone issue?
    – Kasper
    Feb 25, 2020 at 15:17

1 Answer 1

1

Salesforce stores all date-time values in UTC, but displays them in the user's time zone in the UI.

Almost any time you have an N-hour discrepancy between the time you expect and the time you see, it's because you used a local-time value when Salesforce expected UTC or vice versa. Here, it appears that your external application is accepting input in user-local time and then sending it to Salesforce without performing time-zone conversion.

Salesforce interprets this value as UTC, and then attempts to re-convert it to local time when displaying it. This results in an offset from the time value you expected.

Always convert date-times to UTC when sending them to Salesforce via the API, and interpret results from Salesforce APIs in UTC (unless explicitly stated to be in user-local timezone).

4
  • I have done a ToUniversalTime().ToString() before it hits the create method and it doesn't like this format either. Would you have any suggestions, code wise, about how to convert to UTC? I would imagine the datetime picker I am using that uses local time, would be able to get converted to UTC successfully, right?
    – CodyB
    Feb 25, 2020 at 16:00
  • @CodyB I am not a C# programmer so I'm afraid I do not have the domain knowledge to advise you there.
    – David Reed
    Feb 25, 2020 at 16:06
  • No problem, I'll try to figure it out based on the info you gave me, thank you
    – CodyB
    Feb 25, 2020 at 16:58
  • I figured it out, I had to Convert it to Universal Date time using AND THEN format it to the format they want. ToUniversalTime().ToString("s");
    – CodyB
    Feb 25, 2020 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .