Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<!DOCTYPE html>
<html>
    <head>
		<link rel="stylesheet" type="text/css" href="part1.css"/>
    </head>
    <body>
		<section class="dateTime">
	       	<script>
				function printTime()
				{
					var d = new Date();
							
					var years = d.getFullYear();
					var months = d.getMonth() + 1;
					var dates = d.getDate();
							
					document.body.innerHTML = days + ", " + dates + "/" + months + "/" + years;
				}
				setInterval(printTime,1000);
			</script>
		</section>		
    </body>
</html>


What I have tried:

I have create another file named part1.css in same folder in order to link above code and css file together but css file code does not work.Is my linking for external css is correct ?
Posted
Updated 9-Apr-20 7:30am
Comments
gggustafson 8-Apr-20 0:43am    
You are not displaying anything on your page. In your case, the CSS file must be in the same directory as the HTML. But I don't see where CSS is being used. What are you trying to accomplish with the invocation of SetInterval?
Member 14795610 8-Apr-20 2:43am    
setInterval is used to print current date. my css link is inside tag. Is it correct?
Member 14795610 8-Apr-20 2:45am    
Is my way to link css file in my html file correct ? what are the correct way if i am wrong?
gggustafson 8-Apr-20 10:59am    
Link to CSS is OK BUT you don't need CSS. Remove the <link>. Place <title> in the <head>. Why is there a <section>? Remove <section> and its closing tag. What is days? You don't need days. Change the "document.body..." to "document.body.innerHTML = dates + "/" + months + "/" + years;". Change "setInterval(printTime,1000);" to "printTime ( );". Your current code displays (not prints!) the date every second and unless you are executing near midnight the display will not change. I personally would not use setInterval.
Member 14795610 8-Apr-20 22:24pm    
css is used to format the text for date.section is used to put image before the date is loaded.

The first problem that your HTML document does not show anything is because dates is undefined in your JavaScript and breaks the code execution before anything is written to DOM. Maybe you wanted to write this:
JavaScript
let content = "days, " + dates + "/" + months + "/" + years;
There are a bunch of ethical problems with your HTML code. The first one being this:
document.body.innerHTML = days + ", " + dates + "/" + months + "/" + years;
You are using the innerHTML and passing a random string text. Try wrapping the content in an HTML element and then setting it to the innerHTML of body element. This might work:
JavaScript
document.body.innerHTML = "<p>days, " + dates + "/" + months + "/" + years + "</p>";
Now, if you apply some styling to a <p> element, it will be visible if CSS file is loaded properly.
CSS
p {
   color: red;
}
One more thing to note here is that if the color doesn't change, you should check the browser console to see if the file is loaded or there are issues. Use F12 to launch the console.

Check this here: Edit fiddle - JSFiddle - Code Playground[^]
 
Share this answer
 
C#
<!DOCTYPE html>
<html lang="en">
  <head>
    <title></title>
  </head>
  <body>
    <script>
      function 
        {
        var d = new Date();
        var years = d.getFullYear();
        var months = d.getMonth() + 1;
        var dates = d.getDate();

        document.body.innerHTML = dates + "/" + months + "/" + years;
        }
      printTime ( );  
    </script>
  </body>
</html>


By the way, don't use TAB characters. It took me a few minutes to get your code properly indented.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900