Creating Timestamps for Challenges in Cloud Code

This topic explains how to quickly create dates in the format acceptable for the CreateChallengeRequest. The dates are created in UTC time because Challenges use this time to update different clients all over the world across different local time zones.

//Current local time
var now = new Date();

//A day in the future of UTC time
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);

//An hour in the future of UTC time
var inHour = new Date();
inHour.setSeconds(inHour.getHours() + 1);

//One minute in the future of UTC time
var inOneMin = new Date();
inOneMin.setMinutes(inOneMin.getMinutes() + 1);

//Turn to ISO string format
var nowISO = now.toISOString();
var tomorrowISO = tomorrow.toISOString();
var inHourISO = inHour.toISOString();
var inOneMinISO = inOneMin.toISOString();

//Cull the string and add Z at the end to conform to Challenge time format
//Result will look like: "YYYY-MM-DDTHH:MMZ" for example: "2017-06-21T11:18Z"
nowCorrectFormat  = nowISO.substr(0,16) + "Z";
tomorrowCorrectFormat  = tomorrowISO.substr(0,16) + "Z";
inHourCorrectFormat  = inHourISO.substr(0,16) + "Z";
inOneMinCorrectFormat  = inOneMinISO.substr(0,16) + "Z";