/    Sign up×
Community /Pin to ProfileBookmark

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);[/CODE]

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[/CODE]

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.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJul 04.2013 — 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. ?
Copy linkTweet thisAlerts:
@Ronni_designauthorJul 05.2013 — 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);[/CODE]
Copy linkTweet thisAlerts:
@Ronni_designauthorJul 05.2013 — But still this doesnt seem to work. Strange. This code visualizes data for earlier days even if i dont have data for those days
×

Success!

Help @Ronni_design spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...