2

can't seem to get this to work.

 var current_times = new Date();
 var future_times = new Date();
 function time(){
 current_times = current_times.setMinutes(current_times.getMinutes());
 future_times = future_times.setMinutes(future_times.getMinutes() + 1); 
 }

error im getting is: current_times.getMinutes is not a function

note sure this helps, but the time function is called from a function which is initiated on body load.

asked Apr 12, 2011 at 0:00
3
  • 2
    There doesn't seem to be anything wrong with this code. What browser are you using? As I anticipated, I cannot reproduce the problem in Chrome. Commented Apr 12, 2011 at 0:03
  • 1
    1. The specific code you pasted works. 2. Don't setMinute to getMinutes(). Why would you do that?! Commented Apr 12, 2011 at 0:04
  • 1
    post a complete non-working code sample. Commented Apr 12, 2011 at 0:06

2 Answers 2

5

The problem is that setMinutes returns a number, not a Date object.

The function will work the first time you call it, but on the second call current_times and future_times will be numbers, and hence won't have the getMinutes function. Since setMinutes() modifies the Date object instead of producing a new one, the solution is to not reassign your variables.


Furthermore, if I understand your intentions correctly, your code can be simplified to:

var current_times, future_times = new Date();
function time() {
 current_times = new Date();
 future_times.setMinutes(current_times.getMinutes() + 1);
}
answered Apr 12, 2011 at 0:08

1 Comment

good catch, I was thinking that maybe he was declaring those variables in a namespace and nullifying their globalness.
0

the code is right but as Box9 typed code

you must assign variables inside the function to be like that'

function time() {
 var current_times = new Date();
 var future_times = new Date();
 current_times = current_times.setMinutes(current_times.getMinutes());
 future_times = future_times.setMinutes(future_times.getMinutes() + 1); 
 document.write(current_times);
 document.write("<br>"+future_times);
}
answered Apr 12, 2011 at 0:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.