HTML Goodies: Script Tip: Week 32
Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js
Date: 1/31/2018 @ 2 p.m. ET
You should be up to speed at this point with how to present an array of text strings. This script uses two. This time around we'll learn how to extract the text string we want from each array. But first, familiarize yourself with the script and its effect once again.
Here's the code we're worried about today:
RightNow = new Date() var day = DaysofWeek[RightNow.getDay()] var date = RightNow.getDate() var Month = Months[RightNow.getMonth()] var Year = RightNow.getFullYear()
The first line should be very near and dear to your hearts by now. The variable name RightNowis assigned to a "new Date()" object. That new date object contains all the information regarding the current date and time. Now we'll use a couple of method to extract from that Date() object the day, the date, the month, and the year.
Let me explain this first line because once you get this line down, the rest just fall into place:
var day = DaysofWeek[RightNow.getDay()]
The line starts by assigning the variable name dayto the result of what follows.
What follows it the variable name of the first array, DaysofWeek. Now we know that this line will act upon the first array. That's the one that lists the days of the week. (no duh!).
The format is the same as one of the DaysofWeek array item. There are square brackets immediately following the array name and inside those brackets is the code that will return the day of the week: "RightNow.getDay()".
So what's going to happen is, the "RightNow.getDay()" will return a number, zero through 6, representing the day of the week. Let's say the number returned is 4. In the DaysofWeek array, 4 means Thursday. Thus, the variable day now represents "Thursday". When you call on the variable day, the text "Thursday" will be returned.
The next line of code:
var date = RightNow.getDate()
...follows a similar format but is not attached to an array. This is the number day of the month and we want that to remain a number.
The next line follows the array format exactly except this time around we're looking for the month of the year
var Month = Months[RightNow.getMonth()]
Let's say it's May. "RightNow.getMonth()" would return the number 4. That's equal to the text string "May" in the Monthsarray. Thus when you call on the variable name Month, you'll get the text string "May".
The last line of code might be new to you.
var Year = RightNow.getFullYear()
In the past, I've used the older getYear() method to extract the year from the Date() object. Well, that works, but it only returns a two-digit year and with Y2K looming, that's not such a good thing. This new method getFullYear()returns the full four digit year. You should start using it across the board when you want a year displayed.
OK! Now we have all the returns ready to go. We have the day in text, the number date, the month in text, and a four digit year. Now all we need to do is decide what two-letter extension should print after the day number. Of course it has to be the right two letters. You don't want your text to read Tuesday May 11rd, 1999.
That would be bad.
Next Week: Those Two Letters