JavaScript: Working with Date and String Objects, Exercises of Information Technology

Examples of using the date and string objects in javascript to get and display the current date and time in various formats, as well as performing checks on user input in a registration form.

Typology: Exercises

2011/2012

Uploaded on 08/11/2012

duraid
duraid 🇮🇳

4.3

(3)

72 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
E
E-
-C
CO
OM
MM
ME
ER
RC
CE
E
I
IT
T4
43
30
0
V
VU
U
© Copyright Virtual University of Pakistan 69
Lesson 15
JAVA SCRIPTING (CONTINUED….)
Example Date object <HTML> <HEAD><TITLE>Writing the Current Date and Time</TITLE>
</HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"> <!--
document.write(“Welcome! you are visiting my web site on " + Date()) //--> </SCRIPT> </BODY>
</HTML> Result is shown in Fig. 1 below. Here, ‘Date()’ is the constructor of the date object
whichprovides current date of the system.
Fig. 1
Getting date and time in a user friendly format
To get the date/time in a different format, an instance of the date object can be created. In the following
example‘d’ is such an instance. To define the instance d of the date object we have to use a constructor of
the date object, preceded by the word ‘new’. Constructor is defined as the initializing function used to
create instance/copy of an object. It is after the name of the object whose constructor it is. Note that we
can invoke or apply different methods/functions of the date object using this instance ‘d’, e.g, d.getDay(),
d.getYear() etc.
<HTML> <HEAD><TITLE>Example - Current Date and Time</TITLE> </HEAD> <BODY>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"> <!--// Store the date in a variable d =
new Date() dateText = ""// Get the current day and convert it to the name of the day dayValue =
d.getDay() if (dayValue == 0)
dateText += "Sunday" else if (dayValue == 1) dateText += "Monday" else if (dayValue == 2)
dateText += "Tuesday" else if (dayValue == 3) dateText += "Wednesday" else if (dayValue == 4)
dateText += "Thursday" else if (dayValue == 5) dateText += "Friday" else if (dayValue == 6) dateText
+= "Saturday"
// Get the current month and convert it to the name of the month monthValue = d.getMonth() dateText
+= " " if (monthValue == 0)
dateText += "January" if (monthValue == 1) dateText += "February" if (monthValue == 2) dateText +=
"March" if (monthValue == 3) dateText += "April" if (monthValue == 4) dateText += "May" if
(monthValue == 5) dateText += "June"
if (monthValue == 6) dateText += "July" if (monthValue == 7) dateText += "August" if (monthValue ==
8) dateText += "September" if (monthValue == 9) dateText += "October" if (monthValue == 10)
dateText += "November" if (monthValue == 11) dateText += "December"
// Get the current year; if it's before 2000, add 1900 if (d.getYear() < 2000) dateText += " " +
d.getDate() + ", " + (1900 + d.getYear()) else
dateText += " " + d.getDate() + ", " + (d.getYear()) // Get the current minutes minuteValue =
d.getMinutes() if (minuteValue < 10)
minuteValue = "0" + minuteValue // Get the current hours hourValue = d.getHours() // Customize
the greeting based on the current hours if (hourValue < 12)
{
greeting = "Good morning!"
timeText = " at " + hourValue + ":" + minuteValue + " AM"
}
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download JavaScript: Working with Date and String Objects and more Exercises Information Technology in PDF only on Docsity!

Lesson 15 JAVA SCRIPTING (CONTINUED….)

Example Date object Writing the Current Date and Time

Result is shown in Fig. 1 below. Here, ‘Date()’ is the constructor of the date object whichprovides current date of the system.

Fig. 1

Getting date and time in a user friendly format

To get the date/time in a different format, an instance of the date object can be created. In the following example‘d’ is such an instance. To define the instance d of the date object we have to use a constructor of the date object, preceded by the word ‘new’. Constructor is defined as the initializing function used to create instance/copy of an object. It is after the name of the object whose constructor it is. Note that we can invoke or apply different methods/functions of the date object using this instance ‘d’, e.g, d.getDay(), d.getYear() etc. Example - Current Date and Time

else if (hourValue == 12) { greeting = "Good afternoon!" timeText = " at " + hourValue + ":" + minuteValue + " PM" } else if (hourValue < 17) { greeting = "Good afternoon!" timeText = " at " + (hourValue-12) + ":" + minuteValue + " PM" } else { greeting = "Good evening!" timeText = " at " + (hourValue-12) + ":" + minuteValue + " PM" } // Write the greeting, the date, and the time to the page document.write(greeting + " It's " + dateText + timeText) //--> Result is shown in Fig. 2 below. Note that mainly three variables, greeting, dateText and timeText have been used. Also, a number of if statements have been used in order to get customized values.

Fig. 2

Example - String Object

In the following example, ‘str’ and ‘myArray’ are the instances of string and array objects, respectively. The size of the array is 10. Here, charAt() is the function/method of string object. So, charAt(3) would provide the value of the element at the index three. Different other functions of string object have also been used. In the example, str.Split(' ') splits the string on the basis of blank space. After splitting, we assign parts of the string as values for the array. Using the String object Using the String object

Result is shown in Fig. 3 below.

Fig. 3

See Fig. 4 below: Fig. 4

Fig. 4 Secondly, if the user violates the permissible limit of 3-15 characters in the text box for user login, again a pop-up box can confront him with a message as shown in Fig. 5 below.

Fig. 5 Similarly, if the user violates the permissible limit of 3-15 characters in respect of Password, an alert box can inform him about it as shown in Fig. 6 below.

  • Fig.