?
Solved

Convert to Valid Time Variable

Posted on 2003-03-20
6
Medium Priority
?
233 Views
Last Modified: 2011-09-20
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.className="DescOver";
        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"
0
Comment
Question by:swinslow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8175523
This should help:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<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)
alert(datNewDate);

}
//-->
</SCRIPT>


</HEAD>
<BODY onLoad=getTime()>

<P>&nbsp;</P>

</BODY>
</HTML>
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8175542
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
0
 
LVL 1

Accepted Solution

by:
niteshn earned 1000 total points
ID: 8175567
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.
0
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!

 
LVL 63

Expert Comment

by:Zvonko
ID: 8175686
Try this:


<html>
<head>
<script>
 function LoginTime()
     {
       TheTime = (new Date()).getTime()%(24*60*60*1000)/1000;
       LogInTime = "15:30:12".split(':');
       LogInTime=LogInTime[0]*3600+LogInTime[1]*60+LogInTime[2]*1;
       TheDesc.className="DescOver";
       TheDesc.innerHTML="You have been logged in for aproximately " + parseInt(TheTime-LogInTime) + " seconds.";
     }

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



0
 
LVL 4

Author Comment

by:swinslow
ID: 8178332
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%>;
        TheDesc.className="DescOver";
        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 ";
      }
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8180665
That's what the Date.parse() will do for you.

Fritz the Blank
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question