Home > JavaScript > Simple date convertion with javascript

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;
    }
}
Advertisements
Categories: JavaScript
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: