• 0

Need Help: JS array to change a word every 90 days


Question

Hello

 

I'm working on a JavaScript array with several words in it and I want it to cycle to the next word every 90 days (or every 3 months if easier).

 

Say I have an array like the following

 

var word = ['test1', 'test2', 'test3', 'test4'];

 

I'd like to do it by date string so I already have a set of variables setup to show date in the following format:

YYYYMMDD

20171213

 

Any help would be appreciated, I am a bit new to JavaScript and Java in general.

 

edit: current code

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Word Testing</title>
<script>
var word = ['test1', 'test2', 'test3', 'test4'];
var d = new Date();
var day = dom(d);
var m1 = d.getMonth() + 1;
var month = ('0' + m1).slice(-2);
var year = d.getFullYear();

function dom(d) {
	return (d.getDate() < 10 ? '0' : '') + d.getDate();
}

window.onload = function tf() {
	document.getElementById('dateid').innerHTML = year + month + day;
    document.getElementById('wordid').innerHTML = word[0];
};
</script>
</head>

<body>
<p>Current Date: <span id='dateid'></span></p>
<p>word: <span id='wordid'></span></p>
</body>
</html>

 

4 answers to this question

Recommended Posts

  • 0

Let's start by making a date:

let date = new Date();

Then let's get our word:

document.getElementById('wordid').innerHTML = word[Math.round((date.getMonth() + 1) / 3) - 1)]; //Pick a different word every 3 months

As for the readable date string, consider using toLocaleDateString():

document.getElementById('dateid').innerHTML = date.toLocaleDateString();

As for the formatting, see the following options documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString#Using_options

 

 

As for every 30 days instead of every 3 months, see the many solutions/workarounds on stackoverflow: https://stackoverflow.com/questions/8619879/javascript-calculate-the-day-of-the-year-1-366

Or be lazy and use a library like moment.js for everything date related, day of year is as simple as:

let day = moment().dayOfYear();

Then you simply do:

document.getElementById('wordid').innerHTML = word[Math.round(day / 30) - 1)]; //Pick a different word every 30 days

 

  • 0
  On 14/12/2017 at 09:19, Seahorsepip said:

 

As for every 30 days instead of every 3 months, see the many solutions/workarounds on stackoverflow: https://stackoverflow.com/questions/8619879/javascript-calculate-the-day-of-the-year-1-366

Or be lazy and use a library like moment.js for everything date related, day of year is as simple as:

let day = moment().dayOfYear();

Then you simply do:

document.getElementById('wordid').innerHTML = word[Math.round(day / 30) - 1)]; //Pick a different word every 30 days

 

Expand  

Using a well tested Library is never lazy. It is good engineering. It reduces the chances of bugs and saves time to focus on the main purpose of your app. And date calculations can be crazy akin to rolling your own encryption!

 

For reference, NodaTime is great for .NET and it has been ported to Java as JodaTime:

 

https://nodatime.org/

 

http://www.joda.org/joda-time/

 

For JavaScript, I agree with moment.js

 

https://momentjs.com/

 

 

  • 0
  On 18/12/2017 at 08:13, DevTech said:

Using a well tested Library is never lazy. It is good engineering. It reduces the chances of bugs and saves time to focus on the main purpose of your app. And date calculations can be crazy akin to rolling your own encryption!

 

For reference, NodaTime is great for .NET and it has been ported to Java as JodaTime:

 

https://nodatime.org/

 

http://www.joda.org/joda-time/

 

For JavaScript, I agree with moment.js

 

https://momentjs.com/

 

 

Expand  

With lazy I was referring to developers using external JS libraries for almost every single line of code resulting in 30 dependencies for 50 lines of code.

Libraries are great but the moment you use libraries like https://www.npmjs.com/package/sum-of-two-numbers you should start getting worried :shiftyninja:

  • 0
  On 18/12/2017 at 11:08, Seahorsepip said:

With lazy I was referring to developers using external JS libraries for almost every single line of code resulting in 30 dependencies for 50 lines of code.

Libraries are great but the moment you use libraries like https://www.npmjs.com/package/sum-of-two-numbers you should start getting worried :shiftyninja:

Expand  

Yeah but... you made that particular point in the context of Date Calculations which few people realize are a tricky minefield, so it might have been more informative to say something like "if you are one of those minimalists that hate to use libraries, here is an area you should make an exception"

 

But I could have worded my comment better since it is hard to get posts in this sub-forum and quibbling over details when you were providing some great help was mainly stupid on my part.

 

I love that sum function for sheer understated Monty Pythonish afrontery. In keeping with that venerable Bristish tradition it would be nice if we could dream up a pull request on the project! It hasn't been updated in 3 years...

 

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Court orders Apple to keep web links in the App Store, eroding its iOS payment monopoly by Fiza Ali Apple has been ordered to continue permitting web links and external payment options in the App Store after its bid to halt court’s ruling was declined today by a higher court. Earlier this year, in April, a federal judge decreed that Apple must allow developers to include web links in their iOS apps, remove restrictions on link formatting, and enable external payment methods without taking a commission on transactions. Apple immediately appealed and sought an injunction to delay implementation of the order while the case progressed. However, the United States Court of Appeals has now refused Apple’s emergency request to stay the district court’s order. In its decision, the panel held that Apple had not demonstrated a sufficient likelihood of success on appeal, nor that it would suffer irreparable harm if the order were enforced. The court also considered potential prejudice to other parties and the public interest, concluding that an immediate suspension was not warranted. This ruling makes it much harder for Apple to overturn the April decision, which came from a lawsuit initiated by Epic Games. Epic first sued Apple’s App Store policies in 2020, claiming that the company’s restrictions harmed competition. While Epic did not prevail on every count, the court did rule that Apple must allow developers to inform users of alternative purchasing options at better prices. Despite that narrow victory, Apple repeatedly failed to conform to the terms from the original 2021 ruling, prompting the judge in April to issue a more detailed order outlining precisely how the App Store must be “opened up”. In response to the April ruling, prominent third-party apps have swiftly implemented web-based purchasing links. Both Spotify and Amazon’s Kindle app now include buttons directing users to purchase subscriptions via their websites, bypassing Apple’s in-app payments. Additionally, Fortnite has made a comeback on iOS after around five years, presenting users with the choice between Apple’s in-app payment system and Epic’s own payment and rewards mechanism. According to Epic CEO Tim Sweeney, there is presently a 60:40 split in usage favouring Apple’s system over Epic’s, though the gap appears to be narrowing. An Apple spokesperson, Olivia Dalton, issued a statement expressing the company’s disappointment: For now, Apple must comply with the existing injunction. Unless the Appeals Court later overturns the ruling, developers can continue to include web payment links, and Apple’s longstanding monopoly over iOS payment processing may continue to erode. The ultimate resolution will depend on the outcome of the ongoing appeals, which could set a significant precedent for how app marketplaces operate in the future. Source: The Verge
    • Reddit posts are all public, no login(therefore no agreeing to contract) to view the content. It's like the equivalent of sitting in a library and writing down notes from a textbook without signing it out and they start suing you for writing notes.
  • Recent Achievements

    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      397
    2. 2
      +FloatingFatMan
      176
    3. 3
      snowy owl
      170
    4. 4
      ATLien_0
      167
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!