Converting t+# to Date Format Using JavaScript

Here is a useful utility javascript function that converts date in the format t+# or t-# where t is today’s date and # is the days preceding or following today’s date. Value is input in the value field which popluates the result field on blur.

<form name="ipform">
    REGEX VALUE&nbsp;&nbsp;<input type="text" name="regexValue"
        id="regexValue" onBlur="executeValidation()" />
REGEX
    RESULT&nbsp;&nbsp;<input type="text" name="regexResult"
        id="regexResult" />
</form>
<script>
    function executeValidation() {
        var val = document.forms[0].regexValue.value;
        if (val != "") {
            var regex = "^t$|^t[\\+\\-][\\d]+$";

            if (testRegex(val.toLowerCase(), regex)) {
                var d = new Date();
                if (val == 't' || val == 'T') {
                    val = formatDate(d);
                    document.forms[0].regexResult.value = val;
                } else if (val.indexOf("-") != -1) {
                    var dateParse = val.split("-");
                    if (dateParse.length == 2) {
                        d.setDate(d.getDate() - parseInt(dateParse[1]));
                        val = formatDate(d);
                        document.forms[0].regexResult.value = val;
                    }
                } else if (val.indexOf("+") != -1) {
                    var dateParse = val.split("+");
                    if (dateParse.length == 2) {
                        d.setDate(d.getDate() + parseInt(dateParse[1]));
                        val = formatDate(d);
                        document.forms[0].regexResult.value = val;
                    }
                }
            }
        }
    }
    function formatDate(date) {
        var dateString = "";
        if (date) {
            var d = date.getDate();
            var day = (d < 10) ? '0' + d : d;
            if (isNaN(parseInt(day))) {
                dateString = '00/00/0000';
                return dateString;
            }
            var m = date.getMonth() + 1;
            if (isNaN(parseInt(m))) {
                dateString = '00/00/0000';
                return dateString;
            }
            var month = (m < 10) ? '0' + m : m;
            if (isNaN(parseInt(month))) {
                dateString = '00/00/0000';
                return dateString;
            }
            var yy = date.getFullYear();
            if (isNaN(parseInt(yy))) {
                dateString = '00/00/0000';
                return dateString;
            }
            yy += '';

            switch (yy.length) {
            case 0:
                yy = '0000';
                break;
            case 1:
                yy = '000' + yy;
                break;
            case 2:
                yy = '00' + yy;
                break;
            case 3:
                yy = '0' + yy;
                break;
            }
            dateString = month + "/" + day + "/" + yy;
        }
        return dateString;
    }
    function testRegex(val, regExp) {
        var flag = false;
        var re = new RegExp(regExp);
        if (val != "" && val.length > 0 && val.match(re)) {
            flag = true;
        }
        return flag;
    }
</script>

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: