Adding Two Hours to ODK Time

Hi All,

We’ve recently noticed that all the data we’re collecting with the “time” field in ODK is giving us back the information in GMT. Being in South Africa this means our time data is always recorded in Salesforce two hours earlier than what our data collectors entered. I’m looking to write a function to add two hours to entered value, but I’m struggling to get it to work. Here’s the basis of the code as is:

each(
dataPath(“data[*]”),
combine(
upsert(“Reporting__c”,“Unique_ID__c”, fields(
field(“Unique_ID__c”, dataValue(“instanceID”)),

field(“Time_In__c”, dataValue(“time_in”)),
field(“Time_Out__c”, dataValue(“time_out”))

))
)
)

I can definitely help with this! Can you share some state.data so I can see what format time_in is initially?

You’ll likely end up using the Date global in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Hi Taylor,

Sorry for the delay, our time data comes into Open Fn in this format:

Time_In__c: ‘06:00:00.000Z’,
Time_Out__c: ‘11:45:00.000Z’,

Best,

Kayin

Ah, ok—there’s no date. What’s the desired output? (Is that a datetime field in some other system, and are you building that with a date from another input field?)

Yes, sorry, the format is the output of the ODK Time field, so it just has time and time zone.

The data is being input into the new Salesforce “time” field with no date. The formats match fine except that the time zone gets confused.

OK, Javascipt isn’t good with times (no date) and I’m hoping someone comes along with a more elegant solution, but how about this:

field(‘sf_time__c’, function(state) => {
const timeArray = state.data.odk_time.split(’:’);
var hour = parseInt(timeArray[0]);
if (hour < 21) {
hour = hour + 2
} else {
hour = hour + 2 - 24;
}
const strHour = (hour < 10 ? ‘0’ + hour : ‘’ + hour);
return [strHour, timeArray[1], timeArray[2]].join(’:’)
})

Hi Taylor,

It works! The => was causing issues, but with the following it’s working:

field(“Time_In__c”, function(state)
{ const timeArray = state.data.time_in.split(’:’);
var hour = parseInt(timeArray[0]);
if (hour < 21)
{hour = hour + 2}
else {
hour = hour + 2 - 24;}
const strHour = (hour < 10 ? ‘0’ + hour : ‘’ + hour);
return [strHour, timeArray[1], timeArray[2]].join(’:’)
}),
field(“Time_Out__c”, function(state)
{ const timeArray = state.data.time_out.split(’:’);
var hour = parseInt(timeArray[0]);
if (hour < 21)
{hour = hour + 2}
else {
hour = hour + 2 - 24;}
const strHour = (hour < 10 ? ‘0’ + hour : ‘’ + hour);
return [strHour, timeArray[1], timeArray[2]].join(’:’)
}),

Great! And oops, sorry… I was combining that fat arrow syntax with the standard function(arg) { … } syntax.