Format timestamp - Node.js Date

Node.js examples for Date:Time

Description

Format timestamp

Demo Code


/**// ww  w  .java  2 s.c o m
 * User: https://github.com/KotoWolod
 * Date: 2/11/14
 */
/*
Format timestamp
":00" - :frame
"00:" - sec:
"00:00" - sec:frame
"00::" - min::
"00:00:" - min:sec:
"00::00" - min::frame
"00:00:00" - min:sec:frame
"00:::" - hour:::
"00:00::" - hour:min::
"00:00:00:00" - hour:min:sec:frame
"0 frame" - frame
"0 sec" - sec
"0 min" - min
"0 hour" - hour
*/
String.prototype.toFrame = function(){
    var out,frame;
    out = this.split(":");
    !(/^:\d{1,}$/).test(this) || (frame = parseFloat(out[1]));
    !(/^\d{1,}:$/).test(this) || (frame = parseInt(this,10)*24);
    !(/^\d{1,}:\d{1,}$/).test(this) || (frame = (parseFloat(out[0])*24+parseFloat(out[1])));
    !(/^\d{1,}::$/).test(this) || (frame = parseInt(this,10)*24*60);
    !(/^\d{1,}:\d{1,}:$/).test(this) || (frame = (parseFloat(out[0])*24*60+parseFloat(out[1])*24));
    !(/^\d{1,}:\d{1,}:\d{1,}$/).test(this) || (frame = (parseFloat(out[0])*24*60+parseFloat(out[1])*24)+parseFloat(out[2]));
    !(/^\d{1,}::\d{1,}$/).test(this) || (frame = (parseFloat(out[0])*24*60+parseFloat(out[2])));
    !(/^\d{1,}:::$/).test(this) || (frame = parseInt(this,10)*24*60*60);
    !(/^\d{1,}:\d{1,}:\d{1,}:\d{1,}$/).test(this) || (frame = (parseFloat(out[0])*24*60*60+parseFloat(out[1])*24*60+parseFloat(out[2])*24)+parseFloat(out[3]))
    !(/^\d{1,}::\d{1,}:\d{1,}$/).test(this) || (frame = (parseFloat(out[0])*24*60*60+parseFloat(out[2])*24)+parseFloat(out[3]));
    !(/^\d{1,}:::\d{1,}$/).test(this) || (frame = (parseFloat(out[0])*24*60*60+parseFloat(out[3])));
    !(/^\d{1,}:\d{1,}::\d{1,}$/).test(this) || (frame = (parseFloat(out[0])*24*60*60+parseFloat(out[1])*24*60+parseFloat(out[3])));
    !(/^\d{1,}:\d{1,}::$/).test(this) || (frame = (parseFloat(out[0])*24*60*60+parseFloat(out[1])*24*60));
    !(/^\d{1,}:\d{1,}:\d{1,}:$/).test(this) || (frame = (parseFloat(out[0])*24*60*60+parseFloat(out[1])*24*60+parseFloat(out[2])*24));
    !(/^\d{1,}::\d{1,}:$/).test(this) || (frame = (parseFloat(out[0])*24*60*60+parseFloat(out[2])*24));
    !(/^\d{1,}.*frame.*$/).test(this) || (frame = parseInt(this,10));
    !(/^\d{1,}.*sec.*$/).test(this) || (frame = parseInt(this,10)*24);
    !(/^\d{1,}.*min.*$/).test(this) || (frame = parseInt(this,10)*24*60);
    !(/^\d{1,}.*hour.*$/).test(this) || (frame = parseInt(this,10)*24*60*60);
    if(frame===undefined) return false;
    else return frame;
}
String.prototype.toMillisecond = function(){
    return this.toFrame()*(1000/24);
}
Object.prototype.go = function(){
    var self = this, frame;
    this.object = this
    for(key in this){
        this.object[key] = this[key];
        !(key.toString().toFrame()) || (this.object[key.toString().toFrame()]=!(typeof this[key]=== "function") || this[key]);
        frame = key.toString().toFrame()*(1000/24);
        !(typeof this.object[key.toString().toFrame()]==='function' && typeof key.toString().toFrame()=== 'number') || (setTimeout(this.object[key.toString().toFrame()], frame));
    }
    delete this.object;
    return this;
}
Object.prototype.repeat = function(){
    var self = this, frame;
    this.object = this
    for(key in this){
        this.object[key] = this[key];
        !(key.toString().toFrame()) || (this.object[key.toString().toFrame()]=!(typeof this[key]=== "function") || this[key]);
        frame = key.toString().toFrame()*(1000/24);
        !(typeof this.object[key.toString().toFrame()]==='function' && typeof key.toString().toFrame()=== 'number') || (setInterval(this.object[key.toString().toFrame()], frame));
    }
    delete this.object;
    return this;
}

/* Usage
var animation={
    "25 frame start of animation":  function(){console.log("start")},
    "02:00":                        function(){console.log("action1")},
    "03:00":                        function(){console.log("action2")},
    "4 sec - the end of animation. It can be any comment, but must start with a number":   function(){console.log("end")}
}.go();
//-----------------------------------------------------------------------
animation = Object;
new animation({"05:00":function(){console.log("action 3")}}).go();
//----------------------------------------------------------------------
[{
    "06:00":function(){
        console.log("action4")
    },
    "07:00":function(){
        console.log("action5")
    }
}.go()];
[{"3 sec repeat that":function(){console.log("Repeated every 3 seconds")}}.repeat()];
console.log("timestap to frame:","01:00:00:00".toFrame());
console.log("timestap to frame:","01:00:00:00".toMillisecond());
*/

Related Tutorials