Michael Lombardo
Michael Lombardo

Reputation: 457

Make a datetime countdown timer in rails

Hello Im so close to making this datetime counter. Im assuming because im using date new Date in my code that im not getting the code to work? Am i suppose to be using date datetime time? When i make a `puts "#{reservation.due_date}" I get the right duedate so im confused why the counter is not working.

 

<div id="dueDate-counter">
   <table>
    	<tr>
    		<td>days</td>
    		<td>hrs</td>
    		<td>mns</td>
    		<td>secs</td>
    	</tr>
    	<tr>
    		<td id="days"></td>
    		<td id="hours"></td>
    		<td id="minutes"></td>
    		<td id="seconds"></td>
    	</tr>
    								
    </table> 

    	<script type="text/javascript">
    		function countdown() {
    			var now = new Date();
    			var eventDate = new Date(<%= reservation.due_date %>);
    			<%= puts "here is reservation !!!!!!!" %>;
    			<%= puts "#{reservation.due_date}" %>;
    			var currentTime = now.getTime();
    			var eventTime = eventDate.getTime();

    			var remTime = eventTime - currentTime;

    			var s = Math.floor(remTime / 1000);
    			var m = Math.floor(s / 60);
    			var h = Math.floor(m / 60);
    			var d = Math.floor(h / 24);

    			h %= 24;
    			m %= 60;
    			s %= 60;

    			h = (h < 10) ? "0" + h : h;
    			m = (m < 10) ? "0" + m : m;
    			s = (s < 10) ? "0" + s : s;

    			document.getElementById("days").textContent = d;
    			document.getElementById("days").innerText = d;

    			document.getElementById("hours").textContent = h;
    			document.getElementById("minutes").textContent = m;
    			document.getElementById("seconds").textContent = s;

    				setTimeout(countdown, 1000);
    								}

    								countdown();

    							</script>
    							
    						</div>

im getting blank data to show up in the days hours etc when viewing the page. down near the beginning of the script I added eventDate I put the due_date in as a new date.. now remember Its a DateTime format so it has hour minutes seconds in the database. So is this not reading because i made it new Date is there one for datetime?? thank you!!

The reservation.due_date shows up in the db as example: 2017-02-09 00:02:00

Upvotes: 0

Views: 1067

Answers (1)

rails_id
rails_id

Reputation: 8220

You just need wrap an output of due date with quote

var eventDate = new Date("<%= reservation.due_date %>");

If you don't, you will get this error on your console browser

Uncaught SyntaxError: missing ) after argument list

Upvotes: 1

Related Questions