My name is Vasyl Khrystiuk‎ > ‎pub‎ > ‎JavaScript‎ > ‎

javascript Date and Seconds conversions

Tags: javascript_tag

// this code work only for days , hours, minutes  and seconds in obj Date, but you can simply improve it

// Seconds to  Date
var sec = 2764799;
var a = new Date( 0, 0, 0, 0, 0, sec );

// Date to Seconds
var b = (a - new Date( 0, 0, 0, 0, 0, 0 ))/1000;

// Seconds to Hms (if will bigger than 23:59:59 - days will lost but you can simply improve this code =) )
var a = new Date( 0, 0, 0, 0, 0, sec );
var h = a.getHours();
var m = a.getMinutes();
var s = a.getSeconds();
str = ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s);

// msangel 
Comments