• 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

    • I'd switch to the new outlook readily ... if it was not such utter dogshit! Trying to replace the original Outlook with all it's functionality with that knock-off mobile app is downright insulting.
    • Yeah, it seemed like a bunch of 2D decal assets to me, so I didn't think it would be a huge issue to replace them. Assuming, of course, that Sony didn't just decide to settle and pay the artist instead. Do we know if that happened?
    • No worries, I wasn't pointing to your post specifically at all. I was talking in general and building upon your idea.
    • This 2TB NVMe Gen4 SSD is priced just $94 and you also get a free 64GB UHS-I memory card by Sayan Sen A few days ago, we reported on Team Group's T-FORCE G50 4TB NVMe SSD that was up for grabs at just $200 thanks to a promo coupon. Sadly, that deal has expired although you can still WD's SN8100 (Gen5) and SN7100 (Gen4) offerings as they are still running the discount. If you don't have the budget for those or are shopping for lower capacity drives then Crucial's T500 Gen4 drive discount is still live, and you can get them for just $125. And while the G50 4TB deal has expired, Team Group is now offering its 2TB model at its lowest ever price and you also get a free Micro SD card with it. The Team Group G50 is also a TLC (triple level cell) NAND flash SSD, and thus the endurance on the T-FORCE SSD is quite good, as it is rated for 1300 TBW (terabytes written) for the 2TB variant. Its MTBF, or Mean Time Between Failure, is claimed at 3,000,000 hours. The operating temperature is 0~70 C. The G50 does not have a dedicated DRAM cache (only the G50 Pro SKUs have it), but since it is based on NVMe version 1.4 which supports HMB (host memory buffer) technology; thus, the drive can use system memory for caching. In terms of performance, Team Group promises sequential read and write speeds of up to 5000 MB/s and 4500 MB/s, respectively. However, the firm does not disclose random throughput metrics. Get the Team Group G50 at the link below (deal is said to be ending in less than 10 hours): Team Group T-FORCE G50 SSD (TM8FFE002T0C129) + Team Group 64GB Elite microSDXC UHS-I U3, V30, A1, Micro SD with SD Adapter, to 100MB/s (TEAUSDX64GIV30A103): $105.99 + $12 off with promo code SSETA665 (Shipped and Sold by Newegg US) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • First Post
      Ian_ earned a badge
      First Post
    • Explorer
      JaviAl went up a rank
      Explorer
    • Reacting Well
      Cole Multipass earned a badge
      Reacting Well
    • Reacting Well
      JLP earned a badge
      Reacting Well
    • Week One Done
      Rhydderch earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      646
    2. 2
      ATLien_0
      269
    3. 3
      Michael Scrip
      218
    4. 4
      +FloatingFatMan
      184
    5. 5
      Steven P.
      146
  • Tell a friend

    Love Neowin? Tell a friend!