Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,

i am trying to get value from Session Variable in javascript
and want to convert into time format

i have tried is

JavaScript
function SessionTime() {

      var someSessionVariable = new Date('<%= Session["LogInTime"].ToString() %>'); //Date.parseDate(@Session["LogInTime"],"dd/MM/yyyy");
         alert(someSessionVariable);
     document.getElementById("SessionTime").innerHTML = someSessionVariable.toLocaleTimeString();
      window.setTimeout("SessionTime()", 1000); // Here 1000(milliseconds) means one 1 Sec
  }


i am getting invalid date on alert(someSessionVariable);

all i wanted to do is

show a user a elapsed time from the time of login which i am getting in '<%= Session["LogInTime"].ToString() %>'

please help!!!
Posted
Updated 29-Jul-14 21:41pm
v4

Use DateTime.TryParse[^] to convert string value into a DateTime.

Then simply take a difference between current date value and this date time value to get the difference.

You can also use the TimeSpan class to calculate the difference.
Here[^] is an example of difference using TimeSpan.Subtract.
 
Share this answer
 
Comments
Omkaara 30-Jul-14 2:57am    
all of above i have to do in Javascript
Abhinav S 30-Jul-14 3:00am    
No. In your ASP.Net code.
Omkaara 30-Jul-14 3:08am    
above code is in javascript
my requirement is to do it in javascript
Abhinav S 30-Jul-14 3:41am    
Its possible. Javascript has a Date.Parse() method.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse.
Other approaches - http://forums.asp.net/t/1861281.aspx?How+To+convert+string+into+datetime+in+javascript.

Calculating the difference between two dates - http://www.htmlgoodies.com/html5/javascript/calculating-the-difference-between-two-dates-in-javascript.html.
Omkaara 30-Jul-14 4:56am    
i want to start timer from <%= Session["LogInTime"].ToString()
JavaScript
function SessionTime() {

    var someSessionVariable = '<%= Session["LogInTime"].ToString() %>'; //Date.parseDate(@Session["LogInTime"],"dd/MM/yyyy");

    var startTime = new Date(someSessionVariable);
    var endTime = new Date();
    var timeDiff = endTime - startTime;
    timeDiff /= 1000;  // strip the miliseconds

     var secs = Math.round(timeDiff % 60);   // get seconds
    timeDiff = Math.floor(timeDiff / 60);    // remove seconds from the date

    var minutes = Math.round(timeDiff % 60);   // get minutes
     timeDiff = Math.floor(timeDiff / 60);     // remove minutes from the date

    var hours = Math.round(timeDiff % 24);   // get hours
     timeDiff = Math.floor(timeDiff / 24);   // remove hours from the date

    var days = timeDiff;  // the rest of timeDiff is number of days

    document.getElementById("lblSessionTime").innerHTML =days + " Days, " + hours + ":" + minutes + ":" + secs;
    window.setTimeout("SessionTime()", 1000); // Here 1000(milliseconds) means one 1 Sec
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900