How I Found the Best Posting Times on dev.to With NodeJS(+ Usable Code)
Tags: javascript, productivity, node, showdev
Published: 2022-03-03 13:14:17

One thing that bothers a lot of creators(including me) is, well, what if I post at the wrong time? What if I post and my post will get barely any traction and all my effort will go to waste? 
![Sad Pepe](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gys6x92s2memv809iln.gif)

But not to worry, I found a way to get an idea of when the best posting times are for different tags on dev.to!

**************
#The solution

I made a NodeJS script that:
1. Scrapes the dates on which the top 50 posts of the last year for different tags have been posted on.
![Post date](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82tq87fbhajfi1cqwatb.png)
2. Converts the dates to weekdays.
3. Prints the first, second and third best days of the week to post on for a tag.

******************
#The code
Here's the code that does the magic:

**index.js**
```Javascript
const puppeteer = require('puppeteer');
async function bestdays() {
  const browser = await puppeteer.launch({});  
  days = []
  var page = await browser.newPage();
  const args = process.argv.slice(2)
  await page.goto('https://dev.to/t/'+args[0]+'/top/year');    
  for(x = 0;x < 50;x++){
    try{
      let date1 = await page.$eval("#substories > div:nth-child("+x+") > div > div.crayons-story__top > div > div:nth-child(2) > a > time", element=> element.getAttribute("datetime"))
      const d = new Date(date1);
      let day = d.getDay();
      days.push(day)
    }
    catch{}
  }
  function getAllIndexes(arr, val) {
    var indexes = [], i = -1;
    while ((i = arr.indexOf(val, i+1)) != -1){
        indexes.push(i);
    }
    return indexes;
  }
  function max(input) {
    if (toString.call(input) !== "[object Array]")  
      return false;
    return Math.max.apply(null, input);
  }
  var indexes1 = getAllIndexes(days, 0);
  var indexes2 = getAllIndexes(days, 1);
  var indexes3 = getAllIndexes(days, 2);
  var indexes4 = getAllIndexes(days, 3);
  var indexes5 = getAllIndexes(days, 4);
  var indexes6 = getAllIndexes(days, 5);
  var indexes7 = getAllIndexes(days, 6);
  dm = [indexes1.length, indexes2.length, indexes3.length, indexes4.length, indexes5.length, indexes6.length, indexes7.length]
  dm2 = []
  weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
  console.log("\n")
  console.log("Best day: "+weekdays[dm.indexOf(max(dm))]+" with "+max(dm)+" top posts from last year")
  for (j = 0; j < dm.length;j++){
    if (j != dm.indexOf(max(dm))){
      dm2.push(dm[j])
    }
    else{
      dm2[j] = 0
    }
  }
  console.log("Second best day: "+weekdays[dm2.indexOf(max(dm2))]+" with "+max(dm2)+" top posts from last year")
  dm3 = []
  for (j = 0; j < dm2.length;j++){
    if (j != dm2.indexOf(max(dm2))){
      dm3.push(dm2[j])
    }
    else{
      dm3[j] = 0
    }
  }
  console.log("Third best day: "+weekdays[dm3.indexOf(max(dm3))]+" with "+max(dm3)+" top posts from last year")
  console.log("\n")
  await page.close()
  await browser.close()
}
bestdays();
```
Make sure to have [NodeJS](https://nodejs.org/en/) and [Puppeteer](https://www.npmjs.com/package/puppeteer) installed before running the script, then run the code by entering `node index.js` and the tag you want to get the best posting times for (without the #) in your terminal.
For example: `node index.js webdev`.

Once you've ran your script you should see that it outputs something similar to the following.
![Best Days](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/om6r1nch5c28z59bws87.png)

*****************
#Best posting days for 20 of the most popular tags

For people who don't want to setup the script themselves, here are the best posting days for the top 20 most popular tags on dev.to:

###Javascript

Best day: Wednesday with 10 top posts from last year
Second best day: Thursday with 10 top posts from last year
Third best day: Saturday with 10 top posts from last year
**it's a tie**

###Webdev

Best day: Wednesday with 14 top posts from last year
Second best day: Thursday with 11 top posts from last year
Third best day: Friday with 7 top posts from last year

###Tutorial

Best day: Wednesday with 9 top posts from last year
Second best day: Saturday with 9 top posts from last year
Third best day: Thursday with 8 top posts from last year

###React

Best day: Wednesday with 12 top posts from last year
Second best day: Saturday with 10 top posts from last year
Third best day: Thursday with 9 top posts from last year

###Python

Best day: Tuesday with 12 top posts from last year
Second best day: Sunday with 12 top posts from last year
Third best day: Monday with 8 top posts from last year

###Productivity

Best day: Wednesday with 12 top posts from last year
Second best day: Monday with 11 top posts from last year
Third best day: Friday with 10 top posts from last year

###CSS

Best day: Wednesday with 14 top posts from last year
Second best day: Tuesday with 8 top posts from last year
Third best day: Thursday with 8 top posts from last year

###Devops

Best day: Wednesday with 12 top posts from last year
Second best day: Friday with 9 top posts from last year
Third best day: Saturday with 7 top posts from last year

###Discuss

Best day: Friday with 11 top posts from last year
Second best day: Monday with 10 top posts from last year
Third best day: Wednesday with 7 top posts from last year

###Career

Best day: Tuesday with 11 top posts from last year
Second best day: Monday with 9 top posts from last year
Third best day: Wednesday with 8 top posts from last year

###Opensource

Best day: Wednesday with 11 top posts from last year
Second best day: Monday with 9 top posts from last year
Third best day: Friday with 8 top posts from last year

###Node

Best day: Sunday with 14 top posts from last year
Second best day: Tuesday with 8 top posts from last year
Third best day: Thursday with 8 top posts from last year

###HTML

Best day: Wednesday with 11 top posts from last year
Second best day: Tuesday with 9 top posts from last year
Third best day: Thursday with 9 top posts from last year

###Codenewbie

Best day: Wednesday with 12 top posts from last year
Second best day: Monday with 9 top posts from last year
Third best day: Tuesday with 8 top posts from last year

###AWS

Best day: Monday with 12 top posts from last year
Second best day: Wednesday with 10 top posts from last year
Third best day: Tuesday with 9 top posts from last year

###Typescript

Best day: Tuesday with 10 top posts from last year
Second best day: Monday with 8 top posts from last year
Third best day: Saturday with 8 top posts from last year

###Android

Best day: Tuesday with 10 top posts from last year
Second best day: Wednesday with 10 top posts from last year
Third best day: Friday with 7 top posts from last year

###Java

Best day: Sunday with 14 top posts from last year
Second best day: Wednesday with 8 top posts from last year
Third best day: Monday with 7 top posts from last year

###Github

Best day: Monday with 14 top posts from last year
Second best day: Tuesday with 9 top posts from last year
Third best day: Friday with 6 top posts from last year

###Blockchain

Best day: Monday with 9 top posts from last year
Second best day: Friday with 9 top posts from last year
Third best day: Saturday with 9 top posts from last year
**another tie**

****************

That's it for this article, hope it was helpful! Follow for more programming stuff.