Hi! I’m trying to fetch an event from Google Calendar and I wanted to convert the time into a more readable format using moment.js, specifically to:
moment().format('ll'); // Aug 30, 2022
moment().format('LT'); // 12:00 AM
I have this code so far, using unix timestamp instead but I’m not sure how to fetch data from a previous step in the workflow - from the google calendar trigger.
import moment from 'moment';
let date = moment.unix(1661817600);
var currentDate = moment().format('ll');
var startTime = moment().format('LT');
var endTime = moment().format('LT');
console.log(currentDate);
console.log(startTime);
console.log(endTime);
Additionally, is it possible to process time differences say from 1:00PM to 4:00PM and have an output as “3 Hours”?
What I’m trying to achieve is to have 4 datas after this step and use them in the subsequent steps (posting on Discord):
This is definitely possible with a Node.js code step! And you’ve actually already figured out the hardest part which is using the Moment formatting to output the format of the date you need.
To access data from other steps in your workflow, you need to use Step Exports which allow you to use or pass on data to other steps.
Here’s a quick video on how to do this in Node.js:
And here’s a link to the documentation:
As for your Moment time difference question, I encourage you to look at the Moment.js documentation for calculating durations. More specifically the subtraction method might be the most useful for you to start with:
Thank you. I’m able to process and pass data between steps with this code.
import moment from 'moment';
export default defineComponent({
async run({ steps, $ }) {
var currentDate = moment(steps.trigger.context.ts).format('ll');
var startTime = moment(steps.trigger.event.start.dateTime).format('LT');
var endTime = moment(steps.trigger.event.end.dateTime).format('LT');
var start = new moment(startTime, 'hours');
var end = new moment(endTime, 'hours');
var duration = moment.duration(end.diff(start)).as('hours');
$.export('Current Date', currentDate);
$.export('Event Start Time', startTime);
$.export('Event End Time', endTime);
$.export('Shift Duration', duration);
},
})