admin管理员组

文章数量:1296846

I am making a website where if a user logs in, the user is given a certain logout time, where the logout time is defined and the timeleft is obtained from the logout time - server time.

I've already got the logout time and server time via PHP, but I want to display the time left dynamically thus requiring Javascript. I've tried using the following code:

function timerStart(arg1, arg2){
    var counter = setInterval(function() {getTimeLeft(arg1,arg2); }, 1000);
}

function getTimeLeft(arg_endtime, arg_currenttime){
    var endtime = new Date(arg_endtime);
    var curtime = new Date(arg_currenttime);

    var timeleft = new Date(endtime - curtime); //endtime - curtime;

    tlHours = timeleft.getHours();
    tlMinutes = timeleft.getMinutes();
    tlSeconds = timeleft.getSeconds();

    document.getElementById("timeleft").value = tlHours + ":" + tlMinutes + ":" + tlSeconds;

}

Where arg1's value is '2011:10:11 17.30.00' and arg2's value is '2011:10:11 15:55:31' as a string (yes, they are in different format). But when I tried the above codes I get NaN:NaN:NaN. I am very new to Javascript (3 days) and trying to catch up a deadline so a thorough explanation is very appreciated.

I am making a website where if a user logs in, the user is given a certain logout time, where the logout time is defined and the timeleft is obtained from the logout time - server time.

I've already got the logout time and server time via PHP, but I want to display the time left dynamically thus requiring Javascript. I've tried using the following code:

function timerStart(arg1, arg2){
    var counter = setInterval(function() {getTimeLeft(arg1,arg2); }, 1000);
}

function getTimeLeft(arg_endtime, arg_currenttime){
    var endtime = new Date(arg_endtime);
    var curtime = new Date(arg_currenttime);

    var timeleft = new Date(endtime - curtime); //endtime - curtime;

    tlHours = timeleft.getHours();
    tlMinutes = timeleft.getMinutes();
    tlSeconds = timeleft.getSeconds();

    document.getElementById("timeleft").value = tlHours + ":" + tlMinutes + ":" + tlSeconds;

}

Where arg1's value is '2011:10:11 17.30.00' and arg2's value is '2011:10:11 15:55:31' as a string (yes, they are in different format). But when I tried the above codes I get NaN:NaN:NaN. I am very new to Javascript (3 days) and trying to catch up a deadline so a thorough explanation is very appreciated.

Share Improve this question edited Sep 27, 2012 at 10:06 Mutation Person 30.5k18 gold badges100 silver badges165 bronze badges asked Sep 27, 2012 at 10:01 Rangga WiratnoRangga Wiratno 1792 gold badges4 silver badges11 bronze badges 2
  • how are you calling timerStart ? – wroniasty Commented Sep 27, 2012 at 10:05
  • I called via onLoad on the body part. James Wisemen's seems to solve my problem but I just realized my code isn't dynamic, oh well.. – Rangga Wiratno Commented Sep 27, 2012 at 10:30
Add a ment  | 

5 Answers 5

Reset to default 2

is '2011:10:11 17.30.00' and arg2's value is '2011:10:11 15:55:31' change to

is '2011/10/11 17:30:00' and arg2's value is '2011/10/11 15:55:31' ??

http://jsfiddle/8XF3F/7/

The format to the Date() function has to be very specific.

I've mocked it up here: http://jsfiddle/PtGxk/

As you can see of your dates are '/'-separated and your times ':'-seprated, everything is ok.

NaN in Javascript means "Not a Number", so you most likely have a calculation that resultet in not numbers (as javascript often doesn't throw you an error but just goes on). You should try figure out what values endTime and curTime actually have, by either breakpointing it or console.log it.

My guess is that JavaScript's Date object does not like the format you are passing to it. From MDN:

The string should be in a format recognized by the parse method (IETF-pliant RFC 1123 timestamps).

However, the constructor is overloaded to accept a number of different methods of supplying the date. If I were doing it, I would use UNIX time, multiplied by 1000 to put it into milliseconds, which is straightforward to get out of PHP and, in both PHP and JavaScript, more intuitive when you're doing arithmetic on it.

document.getElementById("timeleft").value = parseInt(tlHours) + ":" + parseInt(tlMinutes) + ":" + parseInt(tlSeconds);

本文标签: dateJavascript Getting NaNNaNNaNStack Overflow