0

I have this script that makes a button redirect to a whatsapp page, on the URL (a href) I need to insert the number that's gonna be contacted.

What I need to do is each day a different number fills this URL. Example:

day1 - phonen1,

day2 - phonen2,

...,

day 13 - phonen13,

//(starts over)

day14 - phonen1,

day15 - phonen2,

...

<a id="whatsapp" target="_blank" href="https://api.whatsapp.com/send?phone=5519997820734">Link</a>

<script>

    phones= ["phonen1", "phonen2", ..., "phonen13"];
    document.getElementById("whatsapp").href = "https://api.whatsapp.com/send?phone=5519"+ phones[i] +"";  

</script>
6
  • What issues are you running into? Commented Jul 11, 2018 at 20:12
  • I dont know how to make the array print a different number each day. Commented Jul 11, 2018 at 20:14
  • why can't you use Math.random()? Commented Jul 11, 2018 at 20:15
  • Because it can't be random, it needs to follow the rule as the example I showed. Commented Jul 11, 2018 at 20:18
  • phones[d % phones.length] where d is the day number, starting with d = 0 Commented Jul 11, 2018 at 20:53

4 Answers 4

2

you can use the date object with for loop like this:

<a id="whatsapp" target="_blank" href="https://api.whatsapp.com/send?phone=5519997820734">Link</a>

<script>
   phones= ["phonen1", "phonen2", ..., "phonen13"];
   var d = new Date();
   var todayDate = d.getDate();

   for (var i = todayDate; i > 13; i= i-13) {
     todayDate = todayDate - 13;
   }

   document.getElementById("whatsapp").href = "https://api.whatsapp.com/send?phone=5519"+phones[i] + todayDate;
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

This cant help me, because the array are actually real phone numbers, and also after the 13th day, it begins from position 0 again.
ok you can still use the array and just add a small if statement to correct things out I edited my answer
2

Simple Answer:

You can do this using a Date to count the number of days since the unix epoch, and mod that count by the length of your phones array to get an index that moves to the next item every 24 hours:

let phones = ["phonen1", "phonen2", "phonen3", "phonen4"];

const ms_per_day = 24 * 60 * 60 * 1000;
// (new Date()).getTime() gets the number of ms since 1 January 1970 00:00:00 UTC
// we divide by ms_per_day and floor to get the number of 24-hour cycles (this will increment each UTC day)
let days_since_epoch = Math.floor((new Date()).getTime() / ms_per_day);
// we mod by the length of phones to get a number in the range [0, phones.length)
let phones_index = days_since_epoch % phones.length;

document.getElementById("whatsapp").href = "https://api.whatsapp.com/send?phone=5519" + phones[phones_index];

console.log("Set link to", document.getElementById("whatsapp").href);
<a id="whatsapp" target="_blank" href="https://api.whatsapp.com/send?phone=5519997820734"> Link  </a>

Caveats:

Working with time is complicated. The above method doesn't get the number of days exactly:

Due to the differing lengths of days (due to daylight saving changeover), months and years, expressing elapsed time in units greater than hours, minutes and seconds requires addressing a number of issues and should be thoroughly researched before being attempted.

...and the crossover time is in UTC anyway, so it's non-obvious when the above code will switch numbers (it won't be at midnight). But it will do so once every 24 hours, which should be sufficient for the use case described in the post.

One other caveat is that the number won't actually change until the user refreshes the page and reruns the script.

1 Comment

Best answer so far, ... Good job :)
0

Use the date object to create an index into your array

<a id="whatsapp" target="_blank" href="https://api.whatsapp.com/send?phone=5519997820734">Link</a>

<script>

  var phones= ["phone1","phone2","phone3","phone4","phone5","phone6","phone7","phone8","phone9","phone10","phone11","phone12","phone13","phone14"];
    
  var startOfDay1 = new Date('July 1, 2018 00:00:00');//be aware this is the client timezone
  var diffFromNow = Date.now() - startOfDay1.getTime();//get the difference in ms between now and midnight of "day 1"
  console.log(diffFromNow);
  var diffFromNowDays = diffFromNow/(24*60*60*1000);//turn that into a day value
  var daynum = Math.floor(diffFromNowDays % 14);//constrain to 14 days
  console.log(daynum);//zero based index
    document.getElementById("whatsapp").href = "https://api.whatsapp.com/send?phone=5519"+ phones[daynum] +"";  

</script>

1 Comment

Rather than using a non–standard string that must be parsed by the built-in parser, if you wish to create a date with specific values then pass them directly to the constructor: new Date(2018, 6, 1). It's less to type and avoids the (notoriously unreliable) parser completely. ;-)
0

Ollin's answer is great, but you can use local midnight as follows if you wish. Use the remainder operator % with the number of whole days since a particular point in time, any epoch will do.

If you want to do the changeover at midnight local time, then use local midnight for the epoch and current day. Use Math.round to remove daylight saving effects.

The following will change the value returned from the array at local 00:00:00.001 each day:

// Number of whole local days since date to today
function daysDiff(date) {
  // Copy date, set to start of day
  var d = new Date(+date);
  d.setHours(0,0,0,0);
  // Get start of current day
  var e = new Date();
  e.setHours(0,0,0,0);
  // Return whole day count
  return Math.round((e - d)/8.64e7);
}

// Select item from array based on number of whole days
// from epoch to today. Default is 1 July 2018
function getFromArray(array, epoch) {
  if (!epoch) {
    // If not provided, use 1 July 2018
    epoch = new Date(2018,6,1);
  }
  var d = daysDiff(epoch);
  return array[d % array.length];
}

var nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];

// On local date 12 July 2018 returns 12
console.log(getFromArray(nums));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.