Home > OS >  Generating evenly split time stamps for every email in an array
Generating evenly split time stamps for every email in an array

Time:07-12

I need to generate a send_at time that's divided evenly by the seconds in a working day 6am-6pm.

For the sake of this a list of 500 emails will be used.

var emailList = ["[email protected]", "[email protected]", "[email protected]", ...]; //List of 500 Emails
var currentTime = new Date().getTime(); //Unix Time Stamp for right now
var day = 86400/2; //12 hours in seconds(6am-6pm)
var interval = Math.ceil(day/emailList.length); // Every 86.4 second for 500 rounded to 87

I just can't figure out how to get past this point.

End Goal: I need a function that can take an input value of an array full of emails, divide that arrays.length by the 12 hour work day of 6am-6pm, and then output an array or object of times to send each email and maybe that email as well..

Input: myFuncton(emailList)

Output: [{email: "[email protected]", send_at: 1657559806}, {email: "[email protected]", send_at: 1657559893}, {email: "[email protected]", send_at: 1657559980}]

CodePudding user response:

Run over the array, adding each time the interval for the timestamp.

var emailList = ["[email protected]", "[email protected]", "[email protected]"];
var currentTime = new Date().getTime(); //Unix Time Stamp for right now
var day = 12 * 60 * 60 * 1000;
var interval = Math.ceil(day / emailList.length);

var result = [];
var send_at= currentTime;
emailList.forEach(function(email) {
  result.push({
    email: email,
    send_at: send_at,
    human_time: new Date(send_at)
  })
  send_at = interval;
})
console.log(result)

CodePudding user response:

You can note the startTime and endTime of the time window and then calculate the total timeSpan by subtracting them. Then divide the total time by the number of emails to calculate the intervalLength.

From that, you can calculate the send time for each email with the formula startTime intervalLength * index

const emailList = Array(500).fill().map((_, i) => 'email_' i); // Placeholder email list

const startTime = new Date().setHours(6, 0, 0, 0)
const endTime = new Date(startTime).setHours(18);

const timeSpan = endTime - startTime;
const intervalLength = timeSpan / emailList.length;

const output = emailList.map((email, i) => ({ email, send_at: startTime   intervalLength*i }));

console.log(output);

  • Related