Developer Tips and Tricks from a Cyclr Developer: API Formatting

Updated on by Justin Balcombe

API Economy and API Examples

APIs are complex but useful things and have become the backbone of modern technology.

Fortunately, Cyclr is full of API integration experts on hand to help partners achieve integration success.

Experienced software and Cyclr developer Justin dives into API formatting and more specifically date and time formatting.

Justin Balcombe

Date and time formatting for APIs

As a developer, you’ll often need to pass the date to third-party APIs. Often in a wide range of formats, and sometimes converted to different time zones. Luckily for you, Cyclr supports the moment.js JavaScript library natively to help you solve these problems.

Let’s see some examples of moment.js in use. In all the examples that follow the JavaScript can be pasted into a mergefield, or used as a line in an inline script.

How can I format a date?

To get the current date/time in standard ISO 8601 format enter the following:

Javascript
=moment().format();

How can I get the time in UTC (Coordinated Universal Time)?

Whether times should be passed and stored in the local or universal time zone is a complex issue which could easily be the subject of a future blog entry. For this blog, it’s sufficient to say that moment.js can also return and manipulate UTC time.  

  • Get the date and time in UTC:
Javascript
=moment.utc().format();
  • Get the hour in UTC:
Javascript
=moment.utc().hour();

How can I split the date into days or weeks?

Sometimes instead of passing a date, you’ll need the current day number or the month number. Moment.js can help you there too. 

  • Get the day of the year:
Javascript
=moment().dayOfYear();
  • Get the week of the year:
Javascript
=moment().week();
  • Get the month of the year:
Javascript
=moment().month();

How can I manipulate dates?

With Moment.js you can also manipulate dates and times, for instance adding days, weeks or months.

  • Add three days to the current date time:
Javascript
=moment().add(3,'d').format();
  • Subtract one day:
Javascript
=moment().subtrack(1,'d').format();
  • Subtract two weeks:
Javascript
=moment().subtrack(2,'w').format();
  • Add one month:
Javascript
=moment().add(1,'M').format();

Other ranges can be added or subtracted by changing the interval used. The possible intervals are

  • Years – ‘y’
  • Quarters – ‘Q’
  • Months – ‘M’
  • Weeks – ‘w’
  • Days – ‘d’
  • Hours – ‘h’
  • Minutes – ‘m’
  • Seconds – ‘s’
  • Milliseconds -’ms’

Note that ‘M’ is month, and ‘m’ is minutes.

You can also get the first and last day of the month using moment.js.

  • Get the first day of this month:
Javascript
=moment().startOf('month').format();
  • Get the last day of this month:
Javascript
=moment().endOf('month').format();
  • Get the first day of next month:
Javascript
=moment().add(1,'M').startOf('month').format();

API Formatting Summary

Hopefully, this brief overview has given you a taste of what moment.js allows you to do in regards to API formatting within Cyclr. For further details about API formatting and moment.JS Moment.js | Docs (momentjs.com) as well as using Scripts in Cyclr Scripting | Cyclr Documentation.

Get to know APIs even better

Want to learn more about Cyclr and Embedded iPaaS?

Get in touch and choose whether you want a demo, a free trial or just ask questions our team are ready and waiting to get your integration journey started!

About Author

Avatar for Justin Balcombe

Justin Balcombe

Justin is Cyclr's Product Manager and is an experienced IT Development Manager with a demonstrated history of working in the SaaS and financial services industry.

Ready to start your integration journey?

Recommended by G2 users

Book a demo to see Cyclr in action and start creating integration solutions for your customers