2

I have been trying to get the code to work for more then 2 hours, then I tried this site, well there is alot of code here! I have used each of them. Some are in jQuery while some were pure JavaScript.

Here is two of the code that I have tried:

First code: This code was caught from this link: https://stackoverflow.com/a/2725097/1762944

var dateTimeStr = "17:10:03";
var user_time = DateTime.Parse( dateTimeStr );
var time_now = DateTime.Now;
if( time_now > user_time )
 {
  // your code...
 }

Second code: This one is jQuery. It worked for the asker, and was marked as answer too. It works I have checked the fiddle provided by him! But when I tried the code, it didn't work either. Here:

function time_until() {
 currentTime = Math.floor(jQuery.now() / 1000);
 seconds = eventTime - currentTime;
   if(second == 0){
    //Stop voting
   }
 days = Math.floor(seconds / (60 * 60 * 24));
  $('#time').html('seconds ' + seconds + '<br/>days ' + days);
}

I have tried to edit them both. But they don't work.

Reason why they work: The reason behind this all, is that what I want to do is,

  1. I have a database table which is storing the time for users. I mean when they made an activity such like posting status updates!

  2. I have created a timestamp. Which shows perfectly like this: few seconds ago.

  3. Now what I want is, to update the time to 1 minute ago without reloading the page, like Facebook does. Or whatever the time is, to get updated to the next minute or hour or corresponding.

What I have tried is almost next to everything. But I guess the only issue with my codes is that when I place the DateTime.Now value from Database in it, it shows an error. The error is hidden so I have created an else statement like this:

if(seconds < 60) {
  $("#jstime").html(seconds + "seconds..");
// else if minutes, if hours they are all written..and then an else 
} else {
 $("#jstime").html("Again an error, try again bud");
}

I have a setInterval which keeps on updating the id with the error text.

Here is the code that I am using.

<span id="timeval" title="@timeStamp">@TimeStamp</span></div>
<input type="hidden" id="time" value="9/23/2013 8:10:40 PM" />
<span>Real time: @TimeStamp</span><br>
<span id="update">Updated time: no update till now;</span><br>
<span id="jstime">Time</span>

The timeval is the real time that is being shown to the users, input is the time from where I get the value of the time on each post, the next span just shows the time to me, I mean I used this to check what was the time when the page was loaded, update is the span to show the updated time, jstime can also be used to show the updated time.

4
  • 1
    Have a look at momentjs.com Commented Sep 23, 2013 at 15:41
  • That's good! :) can you tell me..why is this happening? 34.4159 why is this still giving me a digit? while I am using this :) var seconds = Math.floor(curTime - Time) / (60 * 1000); Commented Sep 23, 2013 at 15:45
  • The code in your first link/sample was using .Net DateTime APIs. The JavaScript equivalents are: Date.parse() and Date.now() Commented Sep 23, 2013 at 15:52
  • Hi, a code is working but its giving me a decimal value, you can see that above in my comment! @nkron, any guess why? even though floor should give me nearest whole number. But its giving me a decimal Commented Sep 23, 2013 at 15:53

2 Answers 2

1

The problem is that you stored currentTime in the function you call every n seconds. As it gets looped it will everytime create a new current time.

Make it a global variable so you can loop through the function without creating the time new and use the old time instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I have always been having a universal variable. Can you please help me in this? var seconds = Math.floor(curTime - Time) / (60 * 1000); I am using this. But still I am getting a decimal value. Like this: 34.4159 would Math.floor provide me 34 only?
Math.Floor(curTime - Time) -> correct, this gives you an integer which is rounded DOWN. However you then take this and divide it by (60 *1000) which could be giving you a float again.
Update: to round it all down Math.floor((curTime-Time) / (60*1000));
1

Please go to link on stackoverflow and see my answer on how can we compare two dates along with the time in Javascript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.