2015年11月10日 星期二

JavaScript extended function - 依傳入日期取得民國年

Date.prototype.getROCYear = function () {
    return this.getFullYear() - 1911;
};

JavaScript extended function - 將日期轉換為民國格式日期(字串)

Date.prototype.getROCDate = function (ppcharSeperator) {
    var intYear = this.getROCYear();
    var intMonth = this.getMonth() + 1;
    var intDay = this.getDate();

    ppcharSeperator = ppcharSeperator || '';

    return ('' + (intYear + 1000)).substr(1) + ppcharSeperator + ('' + (intMonth + 100)).substr(1) + ppcharSeperator + ('' + (intDay + 100)).substr(1);

};

JavaScript extended function - 依傳入格式將日期轉換為字串(預設yyyy/MM/dd)

Date.prototype.toString = function (ppstrFormat) {
    var origValue = this;
    ppstrFormat = ppstrFormat || 'yyyy/MM/dd';

    if (!origValue) return;

    if (!origValue instanceof Date) return;

    var arrMonth = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var dict = {
        'yyyy': origValue.getFullYear(),
        'yy': origValue.getFullYear() - 2000,
        'yyy': origValue.getFullYear() - 1911,
        'M': origValue.getMonth() + 1,
        'd': origValue.getDate(),
        'H': origValue.getHours(),
        'm': origValue.getMinutes(),
        's': origValue.getSeconds(),
        'MM': ('' + (origValue.getMonth() + 101)).substr(1),
        'MMM': arrMonth[origValue.getMonth()],
        'dd': ('' + (origValue.getDate() + 100)).substr(1),
        'HH': ('' + (origValue.getHours() + 100)).substr(1),
        'mm': ('' + (origValue.getMinutes() + 100)).substr(1),
        'ss': ('' + (origValue.getSeconds() + 100)).substr(1)
    };

    return ppstrFormat.replace(/(y{2,4}|M{1,3}|dd?|HH?|ss?|mm?)/g, function () {
        return dict[arguments[0]];
    });
};

JavaScript extended function - 將數值轉換為帶千分號字串

Number.prototype.toFormattedString = function (c, d, t) {
    var origValue = this;

    c = isNaN(c = Math.abs(c)) ? 2 : c;
    d = d == undefined ? "." : d;
    t = t == undefined ? "," : t;

    var s = origValue < 0 ? "-" : "",
        i = parseInt(origValue = Math.abs(+origValue || 0).toFixed(c)) + "",
        j = (j = i.length) > 3 ? j % 3 : 0;

    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(origValue - i).toFixed(c).slice(2) : "");
};

JavaScript extended function - 傳回字串是否包含傳入鍵值

String.prototype.Contains = function (ppstrKey) {
    if (typeof ppstrKey == 'string' && ppstrKey != '')
        return this.indexOf(ppstrKey) != -1;
    else
        return true;
};

JavaScript extended function - 傳回字串是否為數值格式

String.prototype.isNumeric = function () {
    return parseFloat(this) == this;
};

JavaScript extended function - 將字串轉換為數值型態

String.prototype.toNumeric = function () {
    return this.isNumeric() ? parseFloat(this) : -1;
};

JavaScript extended function - 傳回字串是否為日期格式

String.prototype.isDate = function (ppcharSeperator) {
    var result = true, origValue = this;
    var intYear = -1, intMonth = -1, intDay = -1;

    ppcharSeperator = ppcharSeperator || '';

    if (ppcharSeperator != '') {
        if (origValue.length == 10) {
            var arrDate = origValue.split(ppcharSeperator);

            if (arrDate.length == 3) {
                intYear = arrDate[0].toNumeric();
                intMonth = arrDate[1].toNumeric() - 1;
                intDay = arrDate[2].toNumeric();
            }
            else
                result = false;
        }
        else
            result = false;
    }
    else {
        if (origValue.length == 8) {
            intYear = origValue.substr(0, 4).toNumeric();
            intMonth = origValue.substr(5, 2).toNumeric() - 1;
            intDay = origValue.substr(7, 2).toNumeric();
        }
        else
            result = false;
    }

    if (result) {
        switch (intMonth) {
            case 0:
            case 2:
            case 4:
            case 6:
            case 7:
            case 9:
            case 11:
                result = (intDay >= 1 && intDay <= 31);
                break;

            case 3:
            case 5:
            case 7:
            case 10:
                result = (intDay >= 1 && intDay <= 30);
                break;

            case 1:
                result = intYear % 4 == 0 ? (intDay >= 1 && intDay <= 29) : (intDay >= 1 && intDay <= 28);
                break;

            default:
                result = false;
                break;
        }
    }

    result = result ? !isNaN(Date.parse(origValue)) : false;

    return result;
};

JavaScript extended function - 傳回字串是否為民國年格式

String.prototype.isROCDate = function (ppcharSeperator) {
    ppcharSeperator = ppcharSeperator || '';
    var origValue = this;
    var result = ppcharSeperator == '' ? (origValue.length == 7 ? true : false) : (origValue.length == 9 ? true : false);

    if (result) {
        var intYear = -1, intMonth = -1, intDay = -1;

        if (ppcharSeperator != '' && origValue.Contains(ppcharSeperator)) {
            var arrDate = origValue.split(ppcharSeperator);

            if (arrDate.length == 3) {
                intYear = arrDate[0].toNumeric();
                intMonth = arrDate[1].toNumeric();
                intDay = arrDate[2].toNumeric();
            }
            else
                result = false;
        }
        else {
            intYear = origValue.substr(0, 3).toNumeric();
            intMonth = origValue.substr(3, 2).toNumeric();
            intDay = origValue.substr(5, 2).toNumeric();
        }

        if (result) {
            if (intYear != -1 && intMonth != -1 && intDay != -1) {
                var strDate = ('' + (intYear + 1911).toString()).substr(0) + '/' + ('' + (intMonth + 100).toString()).substr(1) + '/' + ('' + (intDay + 100).toString()).substr(1);
                result = strDate.isDate('/');
            }
        }
    }

    return result;
};