Translate

2012年11月21日 星期三

TextBox DateTime應用-取得日期區間

ref→http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript
ref→http://netbeansboy.org/2011/09/02/javascript-isdate-function-for-uk/

ref→http://taiwantc.com/js/js_tut_a0.htm
ref→http://www.ezineasp.net/post/Javascript-Convert-String-to-Date.aspx
ref→http://api.jquery.com/val/

ref→http://blog.programmingsolution.net/javascript/casting-jquery-object-to-javascript-object-and-javascript-object-to-jquery-object/


<script type="text/javascript">

        var INTERDAY = 2;
        $("#<%=TimeStart.ClientID%>").bind('change', function (e) {
            var endText = $("#<%=TimeEnd.ClientID%>");
            var edate = GetTextDate(endText);
            var date = GetTextDate($(this));
            if (CheckInInterDate($(this), edate, INTERDAY) == false || edate < date) endText.val(dateToYMD(date, INTERDAY));
        });

        $("#<%=TimeEnd.ClientID%>").bind('change', function (e) {
            var startText = $("#<%=TimeStart.ClientID%>");
            var sdate = GetTextDate(startText);
            var date = GetTextDate($(this));
            if (CheckInInterDate($(this), sdate, INTERDAY) == false || sdate > date) startText.val(dateToYMD(date, -1 * INTERDAY));
        });

        function GetTextDate(obj) {
            if (isDate(obj.val()) == false) obj.val(dateToYMD(new Date(), 0));
            return StringtoDate(obj.val());
        }

        function CheckInInterDate(obj, date, interday) {
            var checkbefore = StringtoDate(obj.val());
            var checkafter = StringtoDate(obj.val());
            checkbefore.setDate(checkbefore.getDate() - interday);
            checkafter.setDate(checkafter.getDate() + interday);
            return checkbefore <= date && date <= checkafter;
        }

        function dateToYMD(date, interday) {
            date.setDate(date.getDate() + interday);
            var d = date.getDate();
            var m = date.getMonth() + 1;
            var y = date.getFullYear();
            return '' + y +'-'+ (m<=9?'0'+m:m) +'-'+ (d<=9?'0'+d:d);
        }

        function StringtoDate(dateStr) {
            var datePat = /^(\d{4})(-)(\d{1,2})(-)(\d{1,2})$/;
            var matchArray = dateStr.match(datePat); // is the format ok?
            if (matchArray == null) return new Date();
            return new Date(matchArray[1] + '/' + matchArray[3] + '/' + matchArray[5])
        }

        function isDate(dateStr) {
            var datePat = /^(\d{4})(-)(\d{1,2})(-)(\d{1,2})$/;
            var matchArray = dateStr.match(datePat); // is the format ok?
            if (matchArray == null) return false;

            day = matchArray[5];
            if (day < 1 || day > 31) return false;

            month = matchArray[3];
            if (month < 1 || month > 12) return false;
            if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) return false;

            year = matchArray[1];
            if (month == 2) { // check for february 29th
                var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
                if (day > 29 || (day == 29 && !isleap)) return false;
            }
            return true; // date is valid
        }
</script>

補充:2012/12/4
發現在IE9版本以下會略有問題:微修及調整之後…
this的用法真的要多加注意/   \



沒有留言:

張貼留言