LanguagesJavaScriptFull text Date

# Full text Date

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Tuh-Ip

We have all of the returns up and ready for display, but there’s one more test before the user gets the effect. It’s always something ain’t it? Here’s the script and its effect once again:

After you write a computer language for a while, you start to find sections of commands that you really enjoy using. In HTML, I really like tables. I think they’re great. In JavaScript, it’s the IF/Else conditional statements. I’m amazed at how flexible and powerful they really are. I used another one in this script. You’ve probably seen it by now. It looks like this:

 ``` if (date == 1 || date == 21 || date == 31) {ender = "st"} else if (date == 2 || date == 22) {ender = "nd"} else if (date == 3 || date == 23) {ender = "rd"} else {ender = "th"} ```

The purpose of the conditional statements above is to look at the number returned from the RightNow.getDate() object.method statement. Remember that we assigned that number return the variable name date.

The purpose of these lines of code is to look at the number and decide what two-letter extension should follow. Let’s think it through logically. Yes, you could set up a condition for every number 1 through 31, but that’s really going overboard.

• There are only three times when the “st” is needed.
• There are only two times when the “nd” is needed.
• There are only two times when the “rd” is needed.
• The rest of the numbers get the “th”.

So we set up the conditions:

 ``` if (date == 1 || date == 21 || date == 31) {ender = "st"} ```

The first statement reads, “if the date is equal to 1 or equal to (notice the double equal signs) 21 or equal to 31, then the variable ender equals <SUP>st</SUP>” That would help us to produce “1st”, “21st”, and “31st”.
You probably already know that the HTML <SUP> makes text appear as the smaller letters up high. You can also set that to <SUB> if you’d like, but I don’t think the effect is quite as nice.

Did you catch that double vertical lines means “or”? That’s a great way of adding multiple conditions that have the same result all in one line of code rather than writing each condition out on its own. If you don’t know, you’ll find that vertical line on the same button as the backslash, usually just above the ENTER key.

As with all If statements, if the first condition does not apply, the script rolls over to the next one in line. It looks like this:

 ``` else if (date == 2 || date == 22) {ender = "nd"} ```

Here we test to see if the date number s either 2 or 22. If so, then ender is equal to <SUP>nd</SUP&gt. If not, we move along…

 ``` else if (date == 3 || date == 23) {ender = "rd"} ```

Is the date 3 or 23? If so, then ender is equal to <SUP>rd</SUP&gt. If not, we move on…

 ``` else {ender = "th"} ```

This is a catch all. We’ve set up a conditional system that tests the date number return over three sets of If statements, if the number meets none of the statements, then this “else” statement handles it. If the number gets this far, and it’s a darn good bet it will as “th” is the most used text, then ender is equal to: <SUP>th</SUP&gt.

Display it!

OK, we’ve got all the parts. Now we’ll use a basic document.write() statement to get it onto the page for all the world to see:

 ``` document.write("Today is " +day+ " " +Month+ " " +date+ ender+ ", " +Year+ ".") ```

Do you got it? Good. Next Week we’ll roll through another script.

Next Time: A Random Banner Script

### Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis