Skip to content

Instantly share code, notes, and snippets.

@dab2020
Created May 22, 2022 12:37
Show Gist options
  • Save dab2020/e0e02b3ee52d1ac3d5c91988dd36be36 to your computer and use it in GitHub Desktop.
Save dab2020/e0e02b3ee52d1ac3d5c91988dd36be36 to your computer and use it in GitHub Desktop.
Finds the ammount of time since a article was published in Vanilla Java Script and also rounds it up into a whole number
<script type = "text/javascript" >
// By @dab2020 on github
// One month Time in ms (milliseconds)
var one_month = 1000 * 60 * 60 * 24 * 30
// To set present_dates to two variables
var present_date = new Date();
// 0-11 is Month in JavaScript
var publish_day = new Date(2021, 1, 1)
// To Calculate the result in milliseconds and then converting into months
var Result = Math.round(present_date.getTime() - publish_day.getTime()) / (one_month);
// To remove the decimals from the (Result) months value
var Final_Result = Result.toFixed(0);
//To display the final_result value
document.write("Published "
+ Final_Result
+ " months ago");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment