Node.js examples for Date:Time
Converts unix time to time code string
/**/* w ww . j a v a 2s. co m*/ * Converts unix time to timecode string * * @param {number} time - Unix time * @param {string} [format=h:m:s.ms] - Timecode format (vars: h,m,s,ms) * * @returns {string} Timecode */ exports.unixToString = function(time, format){ "use strict"; if (typeof time === "undefined") { throw Error("Unix time expected"); } var date = new Date(time); var timecode = format || "h:m:s.ms"; timecode = timecode.replace(/ms/g, Math.floor(date.getUTCMilliseconds().pad() / 10).pad()); timecode = timecode.replace(/s/g, date.getUTCSeconds().pad()); timecode = timecode.replace(/m/g, date.getUTCMinutes().pad()); timecode = timecode.replace(/h/g, date.getUTCHours().pad()); return timecode; }; /** * Adds lead zeros to number * * @param {number} [size=2] - Length of the string result * @returns {string} */ Number.prototype.pad = function(size) { var s = String(this); while (s.length < (size || 2)) { s = "0" + s; } return s; };