www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to match date in Millis in Javascript

  1. #1
    Join Date
    Feb 2013
    Posts
    21

    How to match date in Millis in Javascript

    I am trying to fetch data from a db every 10 mins where the data is inserted with a timestamp. Now using this timestamp i want to visualize another field. The visualization has a step size of 10 mins. So it checks from the start till the stop time to find any match. Here is the code -

    Code:
    while((i += step) < stop) {
        var key = (new Date(i)).getTime();
        var value = key in lookup ? lookup[key].gtse: null;
        values.push(value);
    }
    callback(null, values);
    My problem is that the timestamp which is fetched from DB never matches. Here is some data to make it clear.

    Code:
    The key values - 
        1st iteration - 1372168200000 
        2nd iteration - 1372168800000 
    The database fetched value - 1372786393088
    So in this case the key never matches when it is iterating over lookup. If i dont use step and change the while loop to while((i += 1) < stop) then the browser hangs since there is lot of processing after this.

    Should i manipulate the date before db insertion and change the trailing 5-6 places to zero or should i handle this on the client side?

    I need some advice how to tackle this.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,432

    Question

    The date-time stamp in javascript can be retrieved to an accuracy of milliseconds, but seldom will you find an exact match to any returned valued like this.

    You can get the values of the year, month, date, hours, minutes, seconds and milliseconds with javascript.
    See all the options of the Date() function.

    I'm not exactly sure what it is that you are trying to do with your iterating lookup function.
    Could you expand upon your requirements a bit more?
    Are the timestamps saved from a JS Date() function or from a different server format?
    Are you trying to use a setTimeOut or setTimeInterval to retrieve the information from a record in the DB on the server?
    Requirements are a bit unclear to me.

  3. #3
    Join Date
    Feb 2013
    Posts
    21
    The data that i am fetching from the db is stored in the db in 10 mins interval with a current timestamp in millis. So the db has name, gtse, time as columns.
    Now im using cubism.js to visualize this data. If you see the cubism.js page you will see the visualization. That ticks every second and mine ticks every 10 mins.
    So now there is a start and a stop time for the visualization and for that time interval it fetches data from the db and matches whether it matches to the interval.

    Since it wont match ever i thought of this idea to check if the fetched date is less than the start time and greater than (start-10mins)

    Code:
    while((i += step) < stop) {
    	var key = (new Date(i)).getTime();
            for(var j=0; j<rec24.length; j++){
    		if((rec24[j].receiveddate <= key) && (rec24[j].receiveddate > (key - 600000))){
    			var value = rec24[j].gtse;
    			values.push(value);
    		}
    		else{
    			values.push(null);
    		}
    	}
    }
    callback(null, values);

  4. #4
    Join Date
    Feb 2013
    Posts
    21
    But still this doesnt seem to work. Strange. This code visualizes data for earlier days even if i dont have data for those days

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles