# JavaScript check time passed

• (2 Pages)
• 1
• 2

## 23 Replies - 2036 Views - Last Post: 01 January 2013 - 06:37 AM

### #1 AnimeWarrior

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 28-December 12

# 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.
Is This A Good Question/Topic? 0

## Replies To: JavaScript check time passed

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11341
• Posts: 42,760
• Joined: 27-December 08

## 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.
Was This Post Helpful? 0

### #3 AnimeWarrior

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 28-December 12

## 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.
Was This Post Helpful? 0

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11341
• Posts: 42,760
• Joined: 27-December 08

## 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.
Was This Post Helpful? 0

### #5 AnimeWarrior

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 28-December 12

## Re: JavaScript check time passed

Posted 31 December 2012 - 03:42 PM

So the conditional statement;

```if (date.GetTime()) - (now.GetTime()) <= 300000

```

will be true if the time between Now and Date is less than or equal to 5 minutes
Was This Post Helpful? 0

### #6 AnimeWarrior

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 28-December 12

## 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.
Was This Post Helpful? 0

### #7 andrewsw

• I'm not here to twist your niblets

Reputation: 4699
• Posts: 17,431
• Joined: 12-December 12

## 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.

This post has been edited by andrewsw: 31 December 2012 - 05:17 PM

Was This Post Helpful? 0

### #8 AnimeWarrior

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 28-December 12

## 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
}

```

Was This Post Helpful? 0

### #9 andrewsw

• I'm not here to twist your niblets

Reputation: 4699
• Posts: 17,431
• Joined: 12-December 12

## 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.

This post has been edited by andrewsw: 31 December 2012 - 05:36 PM

Was This Post Helpful? 1

### #10 AnimeWarrior

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 28-December 12

## 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:
```	  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?
Was This Post Helpful? 0

### #11 andrewsw

• I'm not here to twist your niblets

Reputation: 4699
• Posts: 17,431
• Joined: 12-December 12

## Re: JavaScript check time passed

Posted 31 December 2012 - 05:53 PM

Perhaps:

```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

Was This Post Helpful? 1

### #12 AnimeWarrior

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 28-December 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.
```	  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
Was This Post Helpful? 0

### #13 AnimeWarrior

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 28-December 12

## 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?
Was This Post Helpful? 0

### #14 andrewsw

• I'm not here to twist your niblets

Reputation: 4699
• Posts: 17,431
• Joined: 12-December 12

## Re: JavaScript check time passed

Posted 31 December 2012 - 06:23 PM

AnimeWarrior, on 31 December 2012 - 06:19 PM, said:

Still not working. How can I tell what the values of date2 and now are?

Use console.log(date2); and look in your browsers console.
Was This Post Helpful? 0

### #15 AnimeWarrior

• New D.I.C Head

Reputation: 0
• Posts: 44
• Joined: 28-December 12

## 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.
Was This Post Helpful? 0

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }