• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Convert to Valid Time Variable

I have the following function. It is supose to calculate time difference between a static time (that I pull from a database) and the current time.
Since I am pulling the statictime and writing it to the javascript it is a string and not a valid time. How can I convert the static time to a valid time?

Here is my current function with a sample time in it.

      function LoginTime()
        Theday = new Date();
        TheTime = Theday.getTime();
        LogInTime = "15:30:12";
        TheDesc.innerHTML="You have been logged in for aproximately " + parseInt((Theday-LogInTime)/1000);

The way that it is now, I get this result (TheDesc.innerHTML).....
"You have been logged in for aproximately NaN"
1 Solution
This should help:

<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<SCRIPT LANGUAGE=javascript>
function getTime(){
var datNow = new Date();
intMonth = datNow.getMonth() + 1;
intDay = datNow.getDate();
intYear = datNow.getFullYear();
strDateString = intMonth + "/" + intDay + "/" + intYear + " 15:30:12"
var datNewDate = new Date(strDateString)


<BODY onLoad=getTime()>


To get the number of minutes in my code, you would use:

var datDifference = (Date.parse(datNewDate) - Date.parse(datNow))*1000/360

Fritz the Blank
You should try to get the time directly in milliseconds. 15:30:12 is never parsed into a date or a number but a string. and that's why your code doesn't work. Instead try using...
TheTime = TheDate.getMilliseconds();
LoginTime = "283574";
...Convert the time to millisecond using a formatter when you get the time from the database in your server side code.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

ZvonkoSystems architectCommented:
Try this:

 function LoginTime()
       TheTime = (new Date()).getTime()%(24*60*60*1000)/1000;
       LogInTime = "15:30:12".split(':');
       TheDesc.innerHTML="You have been logged in for aproximately " + parseInt(TheTime-LogInTime) + " seconds.";

<body onLoad="LoginTime()">
<div id=TheDesc></div>

swinslowAuthor Commented:
niteshn's answer was the closest and worked the best. To get the accurate time difference I had to use Milliseconds.

Here is exactly what I ended up with.
It is a combination of ASP VBScript and the Javascript..

WhenLoggedIn = Request.Cookies("TechCC")("TheDate") & " " &  Request.Cookies("TechCC")("LogInTime")
        WhenLoggedIn = FormatDateTime(WhenLoggedIn,0)
        MilliSince = ((DateDiff("s","01/01/1970 00:00:00",WhenLoggedIn)) * 1000) + (28800*1000)
'(28800*1000) is to compensate for GMT (-8 hrs)
function LoginTime()
        Theday = new Date();
        TheTime = Theday.getTime();
        LogInTime = <%=MilliSince%>;
        TheDiff = parseFloat(Theday-LogInTime)/1000
        TheSeconds = parseInt(TheDiff%60)
        TheMinutes = parseInt(TheDiff%3600/60)
        TheHours = parseInt(TheDiff/3600)
        TheDesc.innerHTML = "You have been logged in for aproximately "
                            + "<br>" + TheHours + " Hours "
                            + "<br>" + TheMinutes + " Minutes "
                            + "<br>" + TheSeconds + " Seconds ";
That's what the Date.parse() will do for you.

Fritz the Blank

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now