PDA

View Full Version : Resolved Browser JS timestamp compatibility when testing against database timestamp



crobinson42
12-12-2013, 09:18 PM
Hi All,
I'm having a difficult time understanding why browsers don't accept "-" in a timestamp (http://dygraphs.com/date-formats.html). I'm wondering if anyone has a solution for testing mysql database timestamps in javascript, considering the recommendation for javascript dates is to use "/" and the database timestamps "-".

jscheuer1
12-13-2013, 01:40 AM
I think some browsers accept - as a separator in a timestamp that's a string (if it were numbers, that would be a minus sign). I believe the comma is the only acceptable separator for timestamps composed of 2 or more numbers. The slash / is more common for a string. But you have to be sure it's a string. Otherwise / is division.

Always bear in mind that generally there's less type conversion in javascript than in PHP. And that for things like dates and times, things are a little more limited than with PHP. That said, a timestamp from one can easily be converted and/or compared with a timestamp from the other.

crobinson42
12-18-2013, 11:54 PM
Thanks for the info John. Here's the simple solution to my problem is was having in case a newbie runs into the problem:



var unixTimestamp = '2014-12-10 09:10:05';
var unixTime = new Date(unixTimestamp*1000); //convert the unix timestamp into milliseconds
var currentTime = new Date(); //gets current timestamp in milliseconds

//test timestamps

if ( unixTime > currentTime){

}
else if ( unixTime < currentTime ){

}......etc.

jscheuer1
12-19-2013, 12:59 AM
That doesn't look quite right to me. But I'll take your word for it. There are several ways. And it is correct that a pure numeric UNIX time stamp with no separators (like: 1387414696) multiplied by 1000 is equivalent to a javascript one.