Archive

Archive for April, 2012

Simple date convertion with javascript


Following there are few functions in javascript that allow you to forget problems and dismiss boring codes to convert
a date with mysql format coming from some ajax calls using javascript:

PS: to use them you must use also the empty() function derived directly from php:

/** 
* Return the day value from a mysql date/datetime 
*/
function getDay(_mySqlDate) {

    if (empty(_mySqlDate)) return "";

    var _els = _mySqlDate.split("-");
    return _els[2].substring(0, 2);
}

/** 
* Return the month value from a mysql date/datetime 
*/
function getMonth(_mySqlDate) {
    if (empty(_mySqlDate)) return "";

    var _els = _mySqlDate.split("-");
    return _els[1].substring(0, 2);
}

/** 
* Return the year value from a mysql date/datetime 
*/
function getYear(_mySqlDate) {
    if (empty(_mySqlDate)) return "";

    var _els = _mySqlDate.split("-");
    return _els[0].substring(0, 4);
}

/** 
* Return the hours value from a mysql date/datetime 
*/
function getHours(_mySqlDate) {
    if (empty(_mySqlDate)) return "";

    if (_mySqlDate.indexOf(":") >= 0) {
        var _els = _mySqlDate.(" ");

        if (empty(_els[1])) {
            _els[1] = _els[0];
        }

        var _arrTimes = _els[1].split(":");
        return _arrTimes[0];
    } else {
        return "";
    }
}

/** 
* Return the minutes value from a mysql date/datetime 
*/
function getMinutes(_mySqlDate) {
    if (empty(_mySqlDate)) return "";

    if (_mySqlDate.indexOf(":") >= 0) {
        var _els = _mySqlDate.(" ");

        if (empty(_els[1])) {
            _els[1] = _els[0];
        }

        var _arrTimes = _els[1].split(":");
        return _arrTimes[1];
    } else {
        return "";
    }
}

/** 
* Return the seconds value from a mysql date/datetime
 */
function getSeconds(_mySqlDate) {
    if (empty(_mySqlDate)) return "";

    if (_mySqlDate.indexOf(":") >= 0) {
        var _els = _mySqlDate.(" ");

        if (empty(_els[1])) {
            _els[1] = _els[0];
        }

        _arrTimes = _els[1].split(":");
        return _arrTimes[2];
    } else {
        return "";
    }
}

/** 
* Return the date with into version dd/mm/yyyy or dd/mm/yyyy hh:mm:ss 
*/
function getDateTime(_mySqlDate, _printTime, _noMilliseconds) {

    if(_printTime !== false) _printTime = true;
    if(_noMilliseconds !== true) _noMilliseconds = false;

    var _date = getDay(_mySqlDate)+"/"+getMonth(_mySqlDate)+"/"+getYear(_mySqlDate);

    if (_mySqlDate.indexOf(":") >= 0) {

        var _els = _mySqlDate.split(" ");
        if (getDay(_mySqlDate) == "") {
            return false;
        }

        if (_els[1] == "00:00:00") {
            _els[1] = "";
        }

        if (_printTime) {
            if (_noMilliseconds) {
                _times = _els[1].split(":");
                _date += " " + _times[0] + ":" + _times[1];
            } else {
                _date += " " + _els[1];
            }
        }

        return (_date == "00/00/0000") ? "" : _date;
    } else {

        if (getDay(_mySqlDate) == "") {
            return false;
        }

        return (_date == "00/00/0000") ? "" : _date;
    }
}
Categories: JavaScript

Empty function from php to javascript


One of the most used function of php is the empty() function.
Unfortunately it’s not present in javascript and here there is the derived code.
I hope it will help you:

 

function empty (mixed_var) {
    if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || 
        mixed_var === null || mixed_var === false || 
        typeof mixed_var === 'undefined') {
        return true;
    }

    if (typeof mixed_var == 'object') {
        for (key in mixed_var) {
            return false;
        }
        return true;
    }

    return false;
}

 

Categories: JavaScript
%d bloggers like this: