使用 Javascript 的日期格式选项

发布于 2024-12-04 02:39:25 字数 2950 浏览 1 评论 0原文

我有这段代码可以更新日历小部件和输入字段,同时验证日期。我们希望用户能够输入任何类型的 mdy 格式(mdy、mdy 等)。问题是 YUI 日历小部件仅接受 m/d/y 格式。所有其他的都返回 NaN。我尝试了几种格式化日期的方法,但没有得到任何似乎有效的方法。我希望能够在没有大量过分夸大的代码的情况下做到这一点。有人对这里的最佳方法有什么建议吗?这是我的代码:

//CALENDAR --------------------------------------------------------------------------------    
var initCal = function(calendarContainer){      
    if(YAHOO.env.getVersion("calendar")){
        var txtDate = Dom.get("dateOfLoss");
        var myDate = new Date();
        var day = myDate.getDate();
        var month = myDate.getMonth() +1;
        var year = myDate.getFullYear() -1;
        var newDate = month + "/" + day + "/" + year;

        function handleSelect(type, args, obj){
            var dates = args[0];
            var date = dates[0];
            var year = date[0], month = date[1], day = date[2];

            txtDate.value = month + "/" + day + "/" + year;
            aCal.hide();
        }

        function updateCal(){   
            if (!(txtDate.value.match(/((\d{2})|(\d))\/|\-((\d{2})|(\d))\/|\-((\d{4})|(\d{2}))/))) {                
                alert("Enter date in mm/dd/yy or mm/dd/yyyy format.");
            }
            else {
                if (txtDate.value != "") {
                    aCal.select(txtDate.value);
                    var selectedDates = aCal.getSelectedDates();
                    if (selectedDates.length > 0) {
                        var firstDate = selectedDates[0];
                        aCal.cfg.setProperty("pagedate", (firstDate.getMonth() + 1) + "/" + firstDate.getFullYear());
                        aCal.render();
                    }
                    else {
                        alert("Date of Loss must be within the past year.");
                    }

                }
              }






        }

        var aCal = new YAHOO.widget.Calendar(null, calendarContainer, {
            mindate: newDate,
            maxdate: new Date(),
            title: "Select Date",
            close: true
        });

        aCal.selectEvent.subscribe(handleSelect, aCal, true);
        aCal.render();

        Event.addListener("update", "click", updateCal);
        Event.addListener(txtDate, "change", function(e){
            updateCal();

        });

        // Listener to show the 1-up Calendar when the button is clicked
        // Hide Calendar if we click anywhere in the document other than the calendar
        Event.on(document, "click", function(e){
            var el = Event.getTarget(e);
            if(Dom.hasClass(el, "calendarButton"))
                aCal.show(); 
            else if (Dom.hasClass(el, "link-close") || !Dom.isAncestor(calendarContainer, el)) 
                aCal.hide();                
        }); 
    }
    else {
        var successHandler = function() {       
            initCal(calendarContainer);     
        };

        OURPLACE.loadComponent("calendar", successHandler);
    }            
};    

I have this code that updates a calendar widget and input field, while validating the date. We want the user to be able to input any type of m-d-y format (m.d.y, m-d-y, and so on). The problem is the YUI calendar widget only accepts the m/d/y format. All others it returns as NaN. I have tried a couple ways to format the date, but can't get anything that seems to work. I would like to be able to do this with out a lot of overblown code. Does anyone have any suggestions as to the best approach here? Here is my code:

//CALENDAR --------------------------------------------------------------------------------    
var initCal = function(calendarContainer){      
    if(YAHOO.env.getVersion("calendar")){
        var txtDate = Dom.get("dateOfLoss");
        var myDate = new Date();
        var day = myDate.getDate();
        var month = myDate.getMonth() +1;
        var year = myDate.getFullYear() -1;
        var newDate = month + "/" + day + "/" + year;

        function handleSelect(type, args, obj){
            var dates = args[0];
            var date = dates[0];
            var year = date[0], month = date[1], day = date[2];

            txtDate.value = month + "/" + day + "/" + year;
            aCal.hide();
        }

        function updateCal(){   
            if (!(txtDate.value.match(/((\d{2})|(\d))\/|\-((\d{2})|(\d))\/|\-((\d{4})|(\d{2}))/))) {                
                alert("Enter date in mm/dd/yy or mm/dd/yyyy format.");
            }
            else {
                if (txtDate.value != "") {
                    aCal.select(txtDate.value);
                    var selectedDates = aCal.getSelectedDates();
                    if (selectedDates.length > 0) {
                        var firstDate = selectedDates[0];
                        aCal.cfg.setProperty("pagedate", (firstDate.getMonth() + 1) + "/" + firstDate.getFullYear());
                        aCal.render();
                    }
                    else {
                        alert("Date of Loss must be within the past year.");
                    }

                }
              }






        }

        var aCal = new YAHOO.widget.Calendar(null, calendarContainer, {
            mindate: newDate,
            maxdate: new Date(),
            title: "Select Date",
            close: true
        });

        aCal.selectEvent.subscribe(handleSelect, aCal, true);
        aCal.render();

        Event.addListener("update", "click", updateCal);
        Event.addListener(txtDate, "change", function(e){
            updateCal();

        });

        // Listener to show the 1-up Calendar when the button is clicked
        // Hide Calendar if we click anywhere in the document other than the calendar
        Event.on(document, "click", function(e){
            var el = Event.getTarget(e);
            if(Dom.hasClass(el, "calendarButton"))
                aCal.show(); 
            else if (Dom.hasClass(el, "link-close") || !Dom.isAncestor(calendarContainer, el)) 
                aCal.hide();                
        }); 
    }
    else {
        var successHandler = function() {       
            initCal(calendarContainer);     
        };

        OURPLACE.loadComponent("calendar", successHandler);
    }            
};    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不美如何 2024-12-11 02:39:26
var updateCal = function(){         
            if (!(txtDate.value.match(/^(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.]\d\d+$/))) {   

                return;
            }
            //else if ((txtDate.value.match(/^(0?[1-9]|1[012])[- .](0?[1-9]|[12][0-9]|3[01])[- .]\d\d+$/))) {                                               

            //} 
            else {                  
                var changedDate = txtDate.value;
                changedDate = changedDate.replace(/[. -]/g, "/");       
                txtDate.value = changedDate;
                badClaimDate = claimDateWithinPastYear();
                aCal.select(changedDate);                   

我使用正则表达式来确定需要替换哪些分隔符(如果有),并简单地使用 .replace。

var updateCal = function(){         
            if (!(txtDate.value.match(/^(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.]\d\d+$/))) {   

                return;
            }
            //else if ((txtDate.value.match(/^(0?[1-9]|1[012])[- .](0?[1-9]|[12][0-9]|3[01])[- .]\d\d+$/))) {                                               

            //} 
            else {                  
                var changedDate = txtDate.value;
                changedDate = changedDate.replace(/[. -]/g, "/");       
                txtDate.value = changedDate;
                badClaimDate = claimDateWithinPastYear();
                aCal.select(changedDate);                   

I used a RegEx to determine which, if any, delimiters needed to be replaced and simply used .replace.

装纯掩盖桑 2024-12-11 02:39:25

您是否尝试过 http://www.datejs.com/

也许您可以定义一些模式并根据输入进行测试。
如何将字符串转换为日期时间JavaScript 中的格式规范?

Did you tried http://www.datejs.com/?

Maybe you can define some patterns and test agaist the input.
How can I convert string to datetime with format specification in JavaScript?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文