smpte-timecode.js
function ( timeCode, frameRate, dropFrame ) {

        // Make this class safe for use without "new"
        if (!(this instanceof Timecode)) return new Timecode( timeCode, frameRate, dropFrame);

        // Get frame rate
        if (typeof frameRate === 'undefined') this.frameRate = 29.97;
        else if (typeof frameRate === 'number' && frameRate>0) this.frameRate = frameRate;
        else throw new Error('Number expected as framerate');
        if (this.frameRate!==23.976 && this.frameRate!==24 && this.frameRate!==25 && this.frameRate!==29.97 && this.frameRate!==30 &&
            this.frameRate!==50 && this.frameRate!==59.94 && this.frameRate!==60
        ) throw new Error('Unsupported framerate');

        // If we are passed dropFrame, we need to use it
        if (typeof dropFrame === 'boolean') this.dropFrame = dropFrame;
        else this.dropFrame = (this.frameRate===29.97 || this.frameRate===59.94); // by default, assume DF for 29.97 and 59.94, NDF otherwise

        // Now either get the frame count, string or datetime        
        if (typeof timeCode === 'number') {
            this.frameCount = Math.round(timeCode);
            this._frameCountToTimeCode();
        }
        else if (typeof timeCode === 'string') {
            // pick it apart
            var parts = timeCode.match('^([012]\\d):(\\d\\d):(\\d\\d)(:|;|\\.)(\\d\\d)$');
            if (!parts) throw new Error("Timecode string expected as HH:MM:SS:FF or HH:MM:SS;FF");
            this.hours = parseInt(parts[1]);
            this.minutes = parseInt(parts[2]);
            this.seconds = parseInt(parts[3]);
            // do not override input parameters
            if (typeof dropFrame !== 'boolean') {
                this.dropFrame = parts[4]!==':';
            }
            this.frames = parseInt(parts[5]);
            this._timeCodeToFrameCount();
        }
        else if (typeof timeCode === 'object' && timeCode instanceof Date) {
            var midnight = new Date(timeCode.getFullYear(), timeCode.getMonth(), timeCode.getDate(),0,0,0);
            var midnight_tz = midnight.getTimezoneOffset() * 60 * 1000;
            var timecode_tz = timeCode.getTimezoneOffset() * 60 * 1000;
            this.frameCount = Math.round(((timeCode-midnight + (midnight_tz - timecode_tz))*this.frameRate)/1000);
            this._frameCountToTimeCode();
        }
        else if (typeof timeCode === 'object' && timeCode.hours >= 0) {
            this.hours = timeCode.hours;
            this.minutes = timeCode.minutes;
            this.seconds = timeCode.seconds;
            this.frames = timeCode.frames;
            this._timeCodeToFrameCount();
        }
        else if (typeof timeCode === 'undefined') {
            this.frameCount = 0;
        }
        else {
            throw new Error('Timecode() constructor expects a number, timecode string, or Date()');
        }

        this._validate(timeCode);

        return this;
    }
function ( timeCode, frameRate, dropFrame ) {

        // Make this class safe for use without "new"
        if (!(this instanceof Timecode)) return new Timecode( timeCode, frameRate, dropFrame);

        // Get frame rate
        if (typeof frameRate === 'undefined') this.frameRate = 29.97;
        else if (typeof frameRate === 'number' && frameRate>0) this.frameRate = frameRate;
        else throw new Error('Number expected as framerate');
        if (this.frameRate!==23.976 && this.frameRate!==24 && this.frameRate!==25 && this.frameRate!==29.97 && this.frameRate!==30 &&
            this.frameRate!==50 && this.frameRate!==59.94 && this.frameRate!==60
        ) throw new Error('Unsupported framerate');

        // If we are passed dropFrame, we need to use it
        if (typeof dropFrame === 'boolean') this.dropFrame = dropFrame;
        else this.dropFrame = (this.frameRate===29.97 || this.frameRate===59.94); // by default, assume DF for 29.97 and 59.94, NDF otherwise

        // Now either get the frame count, string or datetime        
        if (typeof timeCode === 'number') {
            this.frameCount = Math.round(timeCode);
            this._frameCountToTimeCode();
        }
        else if (typeof timeCode === 'string') {
            // pick it apart
            var parts = timeCode.match('^([012]\\d):(\\d\\d):(\\d\\d)(:|;|\\.)(\\d\\d)$');
            if (!parts) throw new Error("Timecode string expected as HH:MM:SS:FF or HH:MM:SS;FF");
            this.hours = parseInt(parts[1]);
            this.minutes = parseInt(parts[2]);
            this.seconds = parseInt(parts[3]);
            // do not override input parameters
            if (typeof dropFrame !== 'boolean') {
                this.dropFrame = parts[4]!==':';
            }
            this.frames = parseInt(parts[5]);
            this._timeCodeToFrameCount();
        }
        else if (typeof timeCode === 'object' && timeCode instanceof Date) {
            var midnight = new Date(timeCode.getFullYear(), timeCode.getMonth(), timeCode.getDate(),0,0,0);
            var midnight_tz = midnight.getTimezoneOffset() * 60 * 1000;
            var timecode_tz = timeCode.getTimezoneOffset() * 60 * 1000;
            this.frameCount = Math.round(((timeCode-midnight + (midnight_tz - timecode_tz))*this.frameRate)/1000);
            this._frameCountToTimeCode();
        }
        else if (typeof timeCode === 'object' && timeCode.hours >= 0) {
            this.hours = timeCode.hours;
            this.minutes = timeCode.minutes;
            this.seconds = timeCode.seconds;
            this.frames = timeCode.frames;
            this._timeCodeToFrameCount();
        }
        else if (typeof timeCode === 'undefined') {
            this.frameCount = 0;
        }
        else {
            throw new Error('Timecode() constructor expects a number, timecode string, or Date()');
        }

        this._validate(timeCode);

        return this;
    }
function ( timeCode , frameRate , dropFrame ) { // Make this class safe for use without "new" if ( ! ( this instanceof Timecode ) ) return new Timecode ( timeCode , frameRate , dropFrame ) ; // Get frame rate if ( typeof frameRate === 'undefined' ) this . frameRate = 29.97 ; else if ( typeof frameRate === 'number' && frameRate > 0 ) this . frameRate = frameRate ; else throw new Error ( 'Number expected as framerate' ) ; if ( this . frameRate !== 23.976 && this . frameRate !== 24 && this . frameRate !== 25 && this . frameRate !== 29.97 && this . frameRate !== 30 && this . frameRate !== 50 && this . frameRate !== 59.94 && this . frameRate !== 60 ) throw new Error ( 'Unsupported framerate' ) ; // If we are passed dropFrame, we need to use it if ( typeof dropFrame === 'boolean' ) this . dropFrame = dropFrame ; else this . dropFrame = ( this . frameRate === 29.97 || this . frameRate === 59.94 ) ; // by default, assume DF for 29.97 and 59.94, NDF otherwise // Now either get the frame count, string or datetime         if ( typeof timeCode === 'number' ) { this . frameCount = Math . round ( timeCode ) ; this . _frameCountToTimeCode ( ) ; } else if ( typeof timeCode === 'string' ) { // pick it apart var parts = timeCode . match ( '^([012]\\d):(\\d\\d):(\\d\\d)(:|;|\\.)(\\d\\d)$' ) ; if ( ! parts ) throw new Error ( "Timecode string expected as HH:MM:SS:FF or HH:MM:SS;FF" ) ; this . hours = parseInt ( parts [ 1 ] ) ; this . minutes = parseInt ( parts [ 2 ] ) ; this . seconds = parseInt ( parts [ 3 ] ) ; // do not override input parameters if ( typeof dropFrame !== 'boolean' ) { this . dropFrame = parts [ 4 ] !== ':' ; } this . frames = parseInt ( parts [ 5 ] ) ; this . _timeCodeToFrameCount ( ) ; } else if ( typeof timeCode === 'object' && timeCode instanceof Date ) { var midnight = new Date ( timeCode . getFullYear ( ) , timeCode . getMonth ( ) , timeCode . getDate ( ) , 0 , 0 , 0 ) ; var midnight_tz = midnight . getTimezoneOffset ( ) * 60 * 1000 ; var timecode_tz = timeCode . getTimezoneOffset ( ) * 60 * 1000 ; this . frameCount = Math . round ( ( ( timeCode - midnight + ( midnight_tz - timecode_tz ) ) * this . frameRate ) / 1000 ) ; this . _frameCountToTimeCode ( ) ; } else if ( typeof timeCode === 'object' && timeCode . hours >= 0 ) { this . hours = timeCode . hours ; this . minutes = timeCode . minutes ; this . seconds = timeCode . seconds ; this . frames = timeCode . frames ; this . _timeCodeToFrameCount ( ) ; } else if ( typeof timeCode === 'undefined' ) { this . frameCount = 0 ; } else { throw new Error ( 'Timecode() constructor expects a number, timecode string, or Date()' ) ; } this . _validate ( timeCode ) ; return this ; }
Timecode object constructor
@param {number|String|Date|Object} timeCode Frame count as number, "HH:MM:SS(:|;|.)FF", Date(), or object.
@param {number} [frameRate=29.97] Frame rate
@param {boolean} [dropFrame=true] Whether the timecode is drop-frame or not
@constructor
@returns {Timecode} timecode
Timecode object constructor