23 Replies - 2824 Views - Last Post: 01 January 2013 - 06:37 AM
#1
JavaScript check time passed
Posted 31 December 2012 - 03:05 PM
How can I check how long it has been between two date and times.
Replies To: JavaScript check time passed
#2
Re: JavaScript check time passed
Posted 31 December 2012 - 03:06 PM
Moved to Javascript.
The easiest way would be to convert both dates into milliseconds, then calculate the difference.
The easiest way would be to convert both dates into milliseconds, then calculate the difference.
#3
Re: JavaScript check time passed
Posted 31 December 2012 - 03:14 PM
I have to admit Im completely new to Javascript. I will have to google it find out how to convert them.
Actually... how can you convert Dates to milliseconds. I can understand hours to milliseconds but not dates.
Actually... how can you convert Dates to milliseconds. I can understand hours to milliseconds but not dates.
#4
Re: JavaScript check time passed
Posted 31 December 2012 - 03:16 PM
You can create a new Date() object, and invoke the getTime() function on it.
#5
Re: JavaScript check time passed
Posted 31 December 2012 - 03:42 PM
So the conditional statement;
will be true if the time between Now and Date is less than or equal to 5 minutes
if (date.GetTime()) - (now.GetTime()) <= 300000
will be true if the time between Now and Date is less than or equal to 5 minutes
#6
Re: JavaScript check time passed
Posted 31 December 2012 - 04:04 PM
well I tried it but the script wont show. Im going to say I was editing a bot for a web game and now the interface isnt showing up. Im assuming it is because of an error but Im not sure what it is, The above code is the only thing added.
#7
Re: JavaScript check time passed
Posted 31 December 2012 - 04:58 PM
if (date.GetTime() - now.GetTime() <= 300000) {
should work if date and now are both valid Date objects, and date > now.
It should even work as
if (date - now <= 300000) {
Useful
BTW I wouldn't (personally) use date as the name of a Date() - it will only lead to confusion.
should work if date and now are both valid Date objects, and date > now.
It should even work as
if (date - now <= 300000) {
Useful
BTW I wouldn't (personally) use date as the name of a Date() - it will only lead to confusion.
This post has been edited by andrewsw: 31 December 2012 - 05:17 PM
#8
Re: JavaScript check time passed
Posted 31 December 2012 - 05:25 PM
Thanks that code above helped. It is now showing up but it still isnt performing the action if the date is within 5 minutes of now.
var date= new Date() date = unsafewindow.formatDateByUnixTime(rpt.reportUnixTime); var now = new Date(); if (date - now <= 300000){ //ACTION HERE }
#9
Re: JavaScript check time passed
Posted 31 December 2012 - 05:33 PM
I've never seen unsafewindow.formatDateByUnixTime. Does it return the correct JS Date format?
Use your browser's console to output the two values that you are comparing.
Added: Is a Unix timestamp in seconds rather than milliseconds? If so, use getTime() and multiply by 1000.
I also just noticed your incorrect casing of GetTime() in your previous code.
Use your browser's console to output the two values that you are comparing.
Added: Is a Unix timestamp in seconds rather than milliseconds? If so, use getTime() and multiply by 1000.
I also just noticed your incorrect casing of GetTime() in your previous code.
This post has been edited by andrewsw: 31 December 2012 - 05:36 PM
#10
Re: JavaScript check time passed
Posted 31 December 2012 - 05:46 PM
Unix time, or POSIX time, is a system for describing instances in time, defined as the number of seconds that have elapsed since midnight Coordinated Universal Time (UTC), 1 January 1970 - Wikipedia
So I would have to have:
Also I think unsafe window is just the frame of the webpage the game is in.
Edit: Doesnt work... I think the format of Unix time is different format and doesnt accept integer math, it wont multiply. Any suggestions on conversion?
So I would have to have:
var date= new Date() date = unsafewindow.formatDateByUnixTime(rpt.reportUnixTime); var now = new Date(); if ((date*1000) - now <= 300000){
Also I think unsafe window is just the frame of the webpage the game is in.
Edit: Doesnt work... I think the format of Unix time is different format and doesnt accept integer math, it wont multiply. Any suggestions on conversion?
#11
Re: JavaScript check time passed
Posted 31 December 2012 - 05:53 PM
Perhaps:
but you really need to use your browser's console to discover what is going on.
And I'm still assuming that date > now.. and I wouldn't use date as the variable name.
var date = new Date(unsafewindow.formatDateByUnixTime(rpt.reportUnixTime) * 1000); var now = new Date(); if (date.getTime() - now.getTime() <= 300000) {
but you really need to use your browser's console to discover what is going on.
And I'm still assuming that date > now.. and I wouldn't use date as the variable name.
This post has been edited by andrewsw: 31 December 2012 - 05:57 PM
#12
Re: JavaScript check time passed
Posted 31 December 2012 - 06:09 PM
the thing still isnt even loading the interface. Also, the Firefox Console shows no errors or warnings with the script.
Note im using date2 instead of the date. This is because the existing code uses date later on and I dont want to ruin it.
nvm Its loading now... I will test the feature and let you know how it works
var date=unsafewindow.formatDateByUnixTime(rpt.reportUnixTime); var date2 = new Date(unsafewindow.formatDateByUnixTime(rpt.reportUnixTime) * 1000); //CUSTOMJJJ var now = new Date(); if (date2.getTime() - now.getTime() <= 300000){
Note im using date2 instead of the date. This is because the existing code uses date later on and I dont want to ruin it.
nvm Its loading now... I will test the feature and let you know how it works
#13
Re: JavaScript check time passed
Posted 31 December 2012 - 06:19 PM
Still not working. How can I tell what the values of date2 and now are?
#14
Re: JavaScript check time passed
Posted 31 December 2012 - 06:23 PM
#15
Re: JavaScript check time passed
Posted 31 December 2012 - 06:29 PM
Ok date2 is an invalid date. Apparently the Unix to Date conversion wasnt allowed. now however is valid.