0

We have a form capture apps from front end that need to pass the value into a UNIX database system.

Since there is no direct integration available, we use Zapier to do this, and it works well except for date conversion, as since we code it in JSON (via Zapier webhook), the UNIX database system muse conform to Unit Date format (e.g. for DOB 1/1/1970 will need to be written as "DOB" : "/Date(-39600000+1100)/",)

The challenge is, how do I convert the standard DD/MM/YYYY into the above format via zapier (since i use it for the integration) so the database can accept it without issue?

Jepri
  • 3
  • 2
  • I'm a little confused what you're asking - there's no JSON date field. It uses strings and numbers, but not explicit date. Can you update your question to be more clear about what system is expecting a json with a particularly formatted date? – xavdid Mar 15 '18 at 23:49
  • Hi, Thanks. sorry for some confusion. i have ammended the question and hopefully that clear things up. Thanks. – Jepri Mar 17 '18 at 06:33

1 Answers1

1

I'm presuming the date format you are talking about is the ISO date format. It looks like this - 2018-03-16T00:00:00.000Z.

You might want to add an intermediary step to format the date field received from your Form Trigger.

Method 1 - Using Zapier Formatter App:

  1. Add an action after your trigger step and choose the Formatter app. screenshot
  2. Select the Date/Time option screenshot
  3. Choose Format under Transform. In the input field, select the date field from the trigger step.
  4. Setup the to format, from format, and timezone as required. screenshot
  5. Use this field in your next Webhook action.

Zapier has an article on formatting dates here.

Method 2 - Using Code app by Zapier: In case you need more control, you can use the code app by Zapier.

  1. Choose the Code app, select "Run Javascript".
  2. In input data set the date field from your trigger app to a property named 'iDate'. screenshot
  3. In the code section, copy and paste the code below.
  4. In the last line you can change the strJSON text to any of the other formats listed above it.
  5. Use the date field output from the code step in your Webhook action.
var yourDate = inputData.iDate;
var dArr = yourDate.split('/');
var d = Date.parse(`${dArr[2]}-${dArr[1]}-${dArr[0]}`);

//String representation of the Date for JSON
var strJSON = new Date(d).toJSON();
console.log('JSON String Representation: ', strJSON);

//ISO Date Format
var iso = new Date(d).toISOString();
console.log('ISO: ', iso);

//Date String
var str = new Date(d).toDateString();
console.log('Date String: ', str);

//Date String with time
var strTstamp = new Date(d).toString();
console.log('Timestamp String: ', strTstamp);

var output = {date: strJSON};

Update:

You can use the below code in the Code app on Zapier (do the setup as instructed above). This will output the time in the format you specified in your comment -39600000+1100.

var yourDate = inputData.iDate;
var dArr = yourDate.split('/');

//11*60*60*1000 for +11:00 Timezone. It is converting 11 hours into milliseconds.
//If your timezone is +05:30, 5*60*60*1000+(30*60*1000)
var unixTime = new Date(`${dArr[2]}-${dArr[1]}-${dArr[0]} 00:00:00 UTC`).getTime() - (11*60*60*1000);

//If your timezone is not +11:00, swap it below.
var unixWithTZ = `${unixTime}+1100`

console.log('UnixTime: ', unixWithTZ);
console.log('UnixWithTZ: ', unixWithTZ);

var output = {date: unixWithTZ};

Read more about Unix Time here.

KayCee
  • 174
  • 1
  • 11
  • Thanks! but sorry i think my question was misleading. the format that i need is for e.g. if raw form capture a DOB as 1/1/1970, i will need a way to auto format it as code into "DOB" : "/Date(-39600000+1100)/", – Jepri Mar 17 '18 at 06:35
  • @Jepri That looks like UNIX/Epoch time in milliseconds with an offset for the timezone. Ideally, 1/1/1970 should be 0 in Unix time. It is considered for the UTC timezone. Is the timezone always going to be +1100? If that is the case, Use the code block I updated in my answer. – KayCee Mar 17 '18 at 19:29
  • Awesome! Thank you KayCee, the codes works well. I have to do a minor adjustment just due to the UNIX database seems to only capture the date without the time zone factor in place. What end up showing in database is the actual date minus time zone calculation (e.g. a DOB 19/8/1981 will show up as 18/8/1981 instead in actual unix database). Removing the "- (11*60*60*1000) resolve this issue. Great work! Thanks again. – Jepri Mar 19 '18 at 06:32
  • @Jepri Wonderful! If this answer solved your problem, make sure to upvote it and select it as correct ([here](https://stackoverflow.com/help/someone-answers)) – KayCee Mar 19 '18 at 09:38