Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1. Write a JavaScript program to display the current day and time in the following
format.
Today is : Friday.
Current time is : 4 PM : 50 : 22
Sample Solution : HTML Code :
view plaincopy to clipboardprint?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body></body>
</html>
JavaScript Code :
29 }
30 else
31 {
32 hour=12;
33 prepand=' AM';
34 }
35 }
36 nsole.log("Current Time : "+hour + prepand + " : " + minute + " : " + seco
nd);
Explanation :
Declaring a JavaScript date : In JavaScript Date objects are based on a time
value that is the number of milliseconds since 1 January, 1970 UTC. You can
declare a date in the following ways :
new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
The getDay() method is used to get the day of the week for the specified date
according to local time, where 0 represents Sunday. The value returned by
getDay() is an integer corresponding to the day of the week: 0 for Sunday, 1 for
Monday, 2 for Tuesday, and so on.
The getHours() method is used to get the hour for a given date, according to local
time. The value returned by getHours() is an integer between 0 and 23.
The getMinutes() method is used to get the minutes in the specified date
according to local time. The value returned by getMinutes() is an integer between
0 and 59.
The getSeconds() method is used to get the seconds in the specified date
according to local time. The value returned by getSeconds() is an integer
between 0 and 59.
AM and PM : AM stands for 'ante meridiem', which means 'before noon' in Latin,
while PM stands for 'post meridiem', which means 'after noon' in Latin.
12-Hour Periods : Nowadays most clocks are 12-hour clocks they divide the 24
hours in a day into two 12-hour periods.
Ante meridiem: Before noon - Between midnight (0:00) & noon (12:00)
Post meridiem: After noon Between noon (12:00) & midnight (0:00)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p></p>
JavaScript Code :
function print_current_page()
window.print();
Explanation :
window.print() : The window object represents a window containing a DOM
document; the document property points to the DOM document loaded in that
window, window.print() is used to open the Print Dialog to print the current
document.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
</body>
</html>
10
JavaScript Code :
var dd = today.getDate();
3
4
var mm = today.getMonth()+1;
if(dd<10)
8
9
dd='0'+dd;
}
10
11 if(mm<10)
12 {
13
mm='0'+mm;
14 }
15 today = mm+'-'+dd+'-'+yyyy;
16 console.log(today);
17 today = mm+'/'+dd+'/'+yyyy;
18 console.log(today);
19 today = dd+'-'+mm+'-'+yyyy;
20 console.log(today);
21 today = dd+'/'+mm+'/'+yyyy;
22 console.log(today);
Explanation :
Declaring a JavaScript date : In JavaScript Date objects are based on a time
value that is the number of milliseconds since 1 January, 1970 UTC. You can
declare a date in the following ways :
new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
The getDate() method is used to get the day of the month for the specified date
according to local time. The value returned by getDate() is an integer between 1
and 31.
The getMonth() method returns the month in the specified date according to local
time, as a zero-based value (where zero indicates the first month of the year).
The value returned by getMonth() is an integer between 0 and 11. 0 corresponds
to January, 1 to February, and so on.
The getFullYear() method is used to get the year of the specified date according
to local time. The value returned by the method is an absolute number. For dates
between the years 1000 and 9999, getFullYear() returns a four-digit number, for
example, 1985.
1
2
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
</script>
</body>
10
</html>
JavaScript Code :
function animate_string(id)
6
7
setInterval(function ()
10
textNode.data = text;
11 }, 100);
12 }
Explanation :
document.getElementById(id) : Returns a reference to the element by its ID; the
ID is a string which can be used to identify the element; it can be established
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>
JavaScript Code :
console.log('--------------------');
if ( d.getDay() === 0 )
6
7
8
Explanation :
Declaring a JavaScript date : In JavaScript Date objects are based on a time
value that is the number of milliseconds since 1 January, 1970 UTC. You can
declare a date in the following ways :
new Date();
new Date(value);
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
The getDay() method is used to get the day of the week for the specified date
according to local time, where 0 represents Sunday. The value returned by
getDay() is an integer corresponding to the day of the week: 0 for Sunday, 1 for
Monday, 2 for Tuesday, and so on.
<!DOCTYPE html>
<html>
<head>
<title>Guess a number</title>
</head>
<body>
</body>
</html>
JavaScript Code :
if (gnum == num)
alert('Matched');
6
7
else
alert('Not matched, the number was ' + num);
Explanation :
The Math.ceil() function is used to get the smallest integer greater than or equal
to a given number.
The Math.random() function is used to get a floating-point, pseudo-random
number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1
(exclusive), which you can then scale to your desired range.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
</body>
</html>
JavaScript Code :
alert(document.URL);
Explanation :
document.URL : The URL read-only property of the Document interface returns
the document location as a string.