Konstantinos-infogeek
3/19/2015 - 1:17 PM

A simple js class to calculate some time annoying problems! Like detect timezone or combine a time and date strings!

A simple js class to calculate some time annoying problems! Like detect timezone or combine a time and date strings!


var TimeDiff = (function () {

    //Constructor
    function TimeDiff() {

        return {
            "calculatePeriod": calculatePeriod,
            "combineTimeAndDate": combineTimeAndDate,
            "getTimeZone": detectTimeZone
        };
    }

    //More Code
    function calculatePeriod(datetime_str, datetime_end) {

        if (localValidation.isTime(datetime_str) && localValidation.isTime(datetime_end)) {

            var datetime_str = parseDateTimeString(datetime_str);
            var datetime_end = parseDateTimeString(datetime_end);
            
            return (datetime_end - datetime_str)/1000;
        }
        return false;
    }

    function combineTimeAndDate(time_str, date_str) {
        if (localValidation.isTime(time_str) && localValidation.isTime(date_str)) {
            var time_a = parseDateTimeString(time_str);
            var date_a = parseDateTimeString(date_str);

            return +new Date(date_a[2], date_a[1], date_a[0], time_a[0], time_a[1]);
        }
        return false;
    }

    function detectTimeZone() {
        var date = new Date();

        var currentHours = (date.getHours() === 0) ? 24 : date.getHours();
        var currentUTCHours = (date.getUTCHours() === 0) ? 24 : date.getUTCHours();

        return currentHours - currentUTCHours;
    }
    
    function parseDateTimeString(timeString){
        var datetime_a = [];
        
        if(localValidation.isTime(timeString)){
            datetime_a = parse(timeString,":");
            datetime_a[2] = (datetime_a[2]) ? datetime_a[2] : 00; 
            
            return +new Date(0,0,0, datetime_a[0], datetime_a[1], datetime_a[2]);
        }
        
        if(localValidation.isDate(timeString)){
            datetime_a = parse(timeString,"/");           
            
            return +new Date(datetime_a[2],datetime_a[1],datetime_a[0]);
        }
        
        function parse(str, sep){
            var ta = str.split(sep);
            return ta.map(function (a) { return parseInt(a); });
        }
    }

    var localValidation = (function () {
        var timeRegex = "\d{1,2}\:\d{1,2}\:\d{1,2}";
        var dateRegex = "\d{1,2}\/\d{1,2}\/\d{4}";

        //General Validation function
        var validate = function (regex, value) {
            var pattern = new RegExp(regex);
            return pattern.test(value);
        };

        //Named functions for specific Validation
        var validateTime = function (value) {
            return validate(this.timeRegex, value);
        };

        var validateDate = function (value) {
            return validate(this.dateRegex, value);
        };

        //Return observable methods
        return {
            "isTime": validateTime,
            "isDate": validateDate
        };

    }());

    //Return the Constructor
    return TimeDiff;
}());