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
5 Answers
Reset to default 2is '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
版权声明:本文标题:date - Javascript Getting NaN:NaN:NaN - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741641095a2389921.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论