Var new date string

Hi
var food = new Date(“Sat Feb 4 15:05:00 2017”);

I need to help to use this line with automatic get today date but clock will be static how can I do it?

What do you mean by “clock will be static”?

BTW, food is a really bad name for a variable that holds a date.

2 Likes

You can use a timer to update it. Have you learned about using timers and intervals?

1 Like

See this code below.It’s a timer that I make in the liveweave that dynamically shows the present time. The HTML code is this:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<title>HTML5, CSS3 and JavaScript demo</title>
</head>
<body>
<!-- Start your code here -->

<div class="lw">Hello Weaver!</div>

<!-- End your code here -->
</body>
</html>

And the JQuery code is this:

var a=document.createElement('p');
    $('.lw').append(a);

(setInterval(function(){
    
    var hours=new Date().getHours();
    if(hours<10){
        hours="0"+hours;
    }
    var minutes=new Date().getMinutes();
    if(minutes<10){
        minutes="0"+minutes;
    }
    var seconds=new Date().getSeconds();
    if(seconds<10){
        seconds="0"+seconds;
    }
   
    a.innerHTML=hours+":"+minutes+":"+seconds;
    
},1000)); 

First I create a new paragraph element called a.
Then I append this paragraph into the div with classname= lw.
I use the setinterval function to refresh the HTML content of the paragraph every 1000ms or 1 sec.
As you see I create threevariables to take the hours, minutes and seconds.And finally I set these three variables into a.HTML property.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.