Sunday, March 27, 2011

Convert ticks to Date object

I needed to display how many days, hours and minutes are remaining for an event at this instant and the event date being in ticks.

//convert the event day to a date object
var startticks = new Date(ticks * 1000);

//convert today to ticks will be in milliseconds
var todayticks = new Date().getTime();

var diff = startticks - todayticks;
var days = Math.floor(diff/(24*60*60*1000));
var hours = (diff/(60*60*1000)) % 24;
var mins = (diff/(60*1000)) % 60;

Here ticks was in seconds, so multiplying it with 1000 to convert to milliseconds.

No comments:

Post a Comment