SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 32

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

 

TIP!

     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.





And here’s the script itself.

     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

    


Learn to write your own JavaScripts with the

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.