Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

var now = new Date()
var month = now.getMonth()

var ar = new Array(12)
ar[0] = "January"
ar[1] = "February"
ar[2] = "March"
ar[3] = "April"
ar[4] = "May"
ar[5] = "June"
ar[6] = "July"
ar[7] = "August"
ar[8] = "September"
ar[9] = "October"
ar[10] = "November"
ar[11] = "December"

var message = "It is now " + ar[month] + ", my favorite.<BR>"
document.write(message)

The current month is extracted from the now instance which holds the attributes of the current time (after the statement has executed). A static array is then created to hold all months’ names (as strings), matching each name to its corresponding number in JavaScript, starting at zero. This obviously fits the default array structure, featuring the first index as zero. Therefore, no math needs to be done, and the current month, by name, is used to construct a message, obviously a string. The message is then printed as plain HTML.


Warning:  The getMonth attribute returns an incorrect value (off by one) on some versions of Navigator. (MSIE works fine.)


getDate()

Called by a Date object, the getDate() method returns the date as an integer between 1 and 31. For some unknown reason, the first day is not zero. The reason might be that the last date of a specific month varies from the last date of another month, making calculations somewhat more difficult to understand. Here is an example:

var now = new Date()
var year = now.getYear()
var month = now.getMonth()
var date = now.getDate()

if (date < 10)
   var lastDigit = date
else
   var lastDigit = date % 10
var exp = ""

// determine suffix
if (lastDigit == 1)
suf = "st"
else
if (lastDigit == 2)
suf = "nd"
else
if (lastDigit == 3)
suf = "rd"
else
suf = "th"

// array for name of month
var ar = new Array(12)
ar[0] = "January"
ar[1] = "February"
ar[2] = "March"
ar[3] = "April"
ar[4] = "May"
ar[5] = "June"
ar[6] = "July"
ar[7] = "August"
ar[8] = "September"
ar[9] = "October"
ar[10] = "November"
ar[11] = "December"

var formDate = date + suf

// build full date such as "May 5th, 1997"
var totalDate = ar[month] + " " + formDate + ", 19" + year

document.write(totalDate)

This script segment combines all methods learned up to now to display a nicely formatted date. At first, all needed attributes of the previously created instance of the Date object are received and assigned to their corresponding variables (e.g., year, month, date). The last digit of the date (1 - 31) is then assigned to the variable lastDigit. According to the value of lastDigit, the proper suffix is assigned to suf via a nested if - else statement. For the digit 1, the suffix is “st” (1st); for the digit 2, the suffix is “nd” (2nd); for the digit 3, it is “rd” (3rd); for all other digits, it is “th” (5th, 6th, ...). Note that the last else statement associates the “th” suffix with all digits other than 1, 2, and 3. Even the ones ending with a 0 apply to this suffix. An array of month names is created as before. The current date is then combined with its suffix to create a string such as “27th”. This string, along with all the other desired values, is used to build a complete date format, such as “April 24th, 1997.” The full string is printed.

getDay()

This method returns the day of the week as an integer between 0 and 6. The day of the week is calculated according to the other attributes, so this method does not have a corresponding setDay() method. Here is an example:

ar = new Array(7)
ar[0] = "Sunday"
ar[1] = "Monday"
ar[2] = "Tuesday"
ar[3] = "Wednesday"
ar[4] = "Thursday"
ar[5] = "Friday"
ar[6] = "Saturday"

var birthday = new Date("January 3, 1978")
var day = birthday.getDay()
alert("You were born on " + ar[day])

getHours()

The getHours() function returns the number of hours since midnight. That is, it returns the current hour according to the 24-hour clock. Note that the range is 0 - 23, from midnight (0) to 11 PM (23). Here is an example:

var now = new Date()
var hour = now.getHours()
var text = ""

if (hour < 12)
   text = "morning"
else
   if (hour < 16)
  text = "afternoon"
   else
  if (hour < 20)
 text = "evening"
  else
 text = "night"
document.write("Good " + text + "!")

This script segment prints a short greeting according to the time of the day. For example, if it is between 12:00 (noon) and 16:00 (4 PM), it prints “Good afternoon!” It is based on a nested if - else construct.

getMinutes()


Note that some of the methods use a plural form, while others use a singular form. The plural ones are getHours(), getMinutes(), and getSeconds(). Remember that the plural ones are the ones associated with time units that are smaller than or equal to hours. All others use a singular form.


The getMinutes() method returns the minute attribute of a Date instance. The integer returned is always between 0 and 59. Here is a short example to demonstrate the method:

var now = new Date()
var minute = now.getMinutes()
var hour = now.getHours()
var text = "Don't you have an appointment for " + (hour + 1)
text += ":00 ?"
if (minute > 49)
   document.write(text)

At first, the message containing the nearest hour is built. For example, if it is currently 15:55, the message is built with the nearest hour, 16:00. The message is printed if the current time is less than ten minutes from the next hour. Note that if it is 23:59 the hour is presented as 24:00, not 00:00.

getSeconds()

This method returns the seconds of a given Date instance, between 0 and 59. The following example takes advantage of this rapidly changing attribute to display a “randomly” chosen image out of ten different ones:

var now = new Date()
var second = now.getSeconds()
var num = second % 10

var image = new Array()
image[0] = "img1.gif"
image[1] = "img2.gif"
image[2] = "img3.gif"
image[3] = "img4.gif"
image[4] = "img5.gif"
image[5] = "img6.gif"
image[6] = "img7.gif"
image[7] = "img8.gif"
image[8] = "img9.gif"
image[9] = "img10.gif"

var img = "<IMG SRC=\"" + image[num] + "\"HEIGHT=100 WIDTH=100>"
document.write(img)

The modulo operator is used to reduce the 60 different numbers to just ten.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us