JavaScript check time passed

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,784
  • Joined: 12-December 12

Re: JavaScript check time passed

Posted 31 December 2012 - 06:32 PM

Use

var tempDate = unsafewindow.formatDateByUnixTime(rpt.reportUnixTime);
console.log(tempDate);


What does it display?
Was This Post Helpful? 0
  • +
  • -

#17 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: JavaScript check time passed

Posted 31 December 2012 - 06:41 PM

[20:39:01.316] Dec 31, 08:02 PM -Unix Time
[20:40:47.298] Mon Dec 31 2012 20:40:47 GMT-0500 (Eastern Standard Time)- now

Any way to convert these two to the same format?
Was This Post Helpful? 0
  • +
  • -

#18 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,784
  • Joined: 12-December 12

Re: JavaScript check time passed

Posted 31 December 2012 - 06:52 PM

[20:39:01.316] Dec 31, 08:02 PM -Unix Time

This appears to have dropped the year, but maybe it defaults to the current year(?). This is probably the wrong time to be testing these dates!!

But, perhaps more significantly, the time is 8:02 but displays as 8:39?! (Unless this is just the time the console was written to..)

I would create 'now' as the date at 8:03pm to see whether the IF works. If so, then it means there is a discrepancy between the unix-time and local time - or the time as it is stored in 'rpt'.
Was This Post Helpful? 0
  • +
  • -

#19 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: JavaScript check time passed

Posted 31 December 2012 - 07:12 PM

	  var date=unsafewindow.formatDateByUnixTime(rpt.reportUnixTime);
	  var date2 = unsafewindow.formatDateByUnixTime(rpt.reportUnixTime);
	  var now = new Date();
	  now = "Dec 31, 08:02 PM";
	  console.log(date2);
	  console.log(now);
	  if (date2.getTime() - now.getTime() <= 300000){


This displays both times just fine. Now the problem is that I cant get the date2 into a Date. Therefore the .getTime doesnt work for date2.

After reading your last post more carefully I understand what you mean.. The Unix time is the timestamp given to the rpt. This script takes that timestamp from the server. that is what the difference was. But the problem still persists that there is no way I know of to convert the Unix to Date.
Was This Post Helpful? 0
  • +
  • -

#20 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,784
  • Joined: 12-December 12

Re: JavaScript check time passed

Posted 31 December 2012 - 07:15 PM

It appears that your function formatDateByUnixTime is returning a string version of a date, but in a format that is not suitable for creating a JS-Date object.

Unless you have an alternative to formatDateByUnixTime() that returns an acceptable date format, then it looks like you might have to parse the string returned by formatDateByUnixTime(), in order to convert it to a string format that is acceptable to Date().
Was This Post Helpful? 0
  • +
  • -

#21 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: JavaScript check time passed

Posted 31 December 2012 - 07:19 PM

View Postandrewsw, on 31 December 2012 - 07:15 PM, said:

It appears that your function formatDateByUnixTime is returning a string version of a date, but in a format that is not suitable for creating a JS-Date object.

Unless you have an alternative to formatDateByUnixTime() that returns an acceptable date format, then it looks like you might have to parse the string returned by formatDateByUnixTime(), in order to convert it to a string format that is acceptable to Date().


I googled around a bit and found this:
var mmToMonth = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

function showLocalDate(timestamp)
{
  var dt = new Date(timestamp * 1000);
  var mm = mmToMonth[dt.getMonth()];
  return dt.getDate() "-" mm "-" dt.getFullYear() " " dt.getHours() ":" dt.getMinutes();
} 



Based on your experience do you think the above will convert the timestamp into local time?
Was This Post Helpful? 0
  • +
  • -

#22 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,784
  • Joined: 12-December 12

Re: JavaScript check time passed

Posted 31 December 2012 - 07:27 PM

That code includes

var dt = new Date(timestamp * 1000);

which you have already tried.

I don't know where your function formatDateByUnixTime() is coming from, but perhaps there are alternative versions to this.

Or see what the original source is:

console.log(rpt.reportUnixTime);
Was This Post Helpful? 0
  • +
  • -

#23 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: JavaScript check time passed

Posted 01 January 2013 - 06:01 AM

[07:59:51.475] 1357045159
That is the raw UnixTime before it is formatted.
Was This Post Helpful? 0
  • +
  • -

#24 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,784
  • Joined: 12-December 12

Re: JavaScript check time passed

Posted 01 January 2013 - 06:37 AM

var date2 = new Date(rpt.reportUnixTime * 1000);
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2