Write an html page called jsTimeDemohtml that contains a but

Write an html page called jsTimeDemo.html that contains a button that, when you click on it, it displays the current date and time in an alert box. The date and time should be displayed as follows: Good morning, it is 7:07 AM on Thursday, July 7, 2016. Note the greeting will designate between morning, afternoon, and evening. A couple more notes on this one: the body of your html page should look like this:


<body>
<input type = “button” value = \"Click here to display date\" OnClick =\"NameOfFunctionYouWroteToDisplayTheDate()\">
</body>

Where “NameOfFunctionYouWroteToDisplayTheDate()” will be replaced with the (hopefully) shorter name you gave to the function you will write to display the date. Hint: to convert the month and the day from numbers to strings, you will use 2 long if…else if statements. For example, if the variable currday holds a number from 0-6 representing the day of the week, you might have something saying if the currday is 0, the currdaystring is “Sunday”, etc. You will do the same with the greetingstring if the currhour is between 0 and 12 it\'s morning, between 12 and 18 it\'s afternoon, else it\'s evening.

Solution

<!--jsTimeDemo.html->
<body>
<input type = \"button\" value = \"Click here to display date\" OnClick =\"displaydate()\">
</body>
<script>

function displaydate()
{
           /* varible used in function */
                var wish;
               var day;
                var monthNames = [\"January\", \"February\", \"March\", \"April\", \"May\", \"June\",
\"July\", \"August\", \"September\", \"October\", \"November\", \"December\"
];
             
                var date = new Date();                          //Create Date current Date Object
              
             
           /* Begin Wish Part */
               var hours = date.getHours();
               if(hours>=0&&hours<12)
               {
                   wish =\"Good Morning\";
               }
               else if(hours>=12&&hours<18)
               {
                   wish = \"Good Afternoon\";
               }
               else
               {
                    wish = \"Good Evening\";
               }
               /* End Wish Part */
              
               /* Begin code for finding weekday */
               var nday = date.getDay();
               if(nday==0)
               day=\"Sunday\";
               else if(nday==1)
               day=\"Monday\";
               else if(nday==2)
               day=\"Tuesday\";
               else if(nday==3)
               day=\"Wednesday\";
               else if(nday==4)
               day=\"Thursday\";
               else if(nday==5)
               day=\"Friday\";
               else if(nday==6)
               day=\"Saturday\";
                /* End code for finding weekday */

               /* Begin code for converting time in 12hrs format */
               var month = date.getUTCMonth() + 1; //months from 1-12
               var monthname= monthNames[date.getMonth()];
               var day1 = date.getUTCDate();
               var year = date.getUTCFullYear();

               var newdate = monthname+\" \"+day1+\",\"+year ;

               var minutes = date.getMinutes();
               var ampm = hours >= 12 ? \'PM\' : \'AM\';
               hours = hours % 12;
               hours = hours ? hours : 12; // the hour \'0\' should be \'12\'
               minutes = minutes < 10 ? \'0\'+minutes : minutes;
               var strTime = hours + \':\' + minutes + \' \' + ampm;

               /* End code for converting time in 12hrs format */
              
                  alert(wish +\",it is \"+strTime+\" on \"+day+\",\"+newdate);                               //display date according to required format


}
</script>

Output : On button click alert will be dispalyed with text like \"Good morning, it is 7:07 AM on Thursday, July 7, 2016\"

Write an html page called jsTimeDemo.html that contains a button that, when you click on it, it displays the current date and time in an alert box. The date and
Write an html page called jsTimeDemo.html that contains a button that, when you click on it, it displays the current date and time in an alert box. The date and

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site