在Mozilla Firefox中使用javascript的日期比较不起作用?Plz帮助我 [英] Date Comparison using javascript in Mozilla firefox is not working?Plz help me

查看:49
本文介绍了在Mozilla Firefox中使用javascript的日期比较不起作用?Plz帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将开始日期与今天的日期和结束日期与开始日期进行比较。此功能在Google Chrome中正常运行但在Mozilla Firefox中不起作用为什么???

日期格式为dd -MMM-yyyy

I have to compare start date with today date and end date with start date .This function works fine in Google chrome but does not works in Mozilla firefox Why???
Dates are in Format dd-MMM-yyyy

function DateCheck(element, name) {
            var todaydate = new Date();
            var year = new Date().getFullYear();
            var month = new Date().getMonth();
            var firstDate = new Date(year, month, 1);
            var dateString1 = (document.getElementById("<%= txtStartDate.ClientID %>").value)
            var myDate1 = new Date(dateString1);
            var dateString2 = (document.getElementById("<%= txtEndDate.ClientID %>").value)
            var myDate2 = new Date(dateString2);
            if (myDate1 < todaydate) {
                alert('Start date must be greater than Today date.');
                document.getElementById("<%= txtStartDate.ClientID %>").value = "";
                return;
            }
            else if (myDate1 > myDate2) {
                alert('End date must be greater than Start date.');
                document.getElementById("<%= txtEndDate.ClientID %>").value = "";
                return;
            }
        }

推荐答案

您传递的是表示日期的字符串。没有一些帮助,Date对象无法理解它...

但是JavaScript Data对象有一个帮助方法 - 解析() [ ^ ] .. 。

使用它你可以将你的字符串转换为JavaScript日期...

You are passing a string that represents a date. Date object can not understand it without some help...
However JavaScript Data object has a helper method - parse()[^]...
Using it you can convert your string to JavaScript date...
var myDate1 = new Date(Date.parse(dateString1));


您可以使用
YOURDATE.replace(/-/gm, '/')



instead of

Date.parse(YOURDATE.replace('-', '/').replace('-', '/')


Javascript is best but weird.

Try following and you will not get Invalid date in any browser.

var NEWDATE = Date.parse(YOURDATE.replace('-', '/').replace('-', '/'));
                if (!isNaN(NEWDATE)) {
                    NEWDATE= new Date(NEWDATE);
                   console.log(NEWDATE.getDate());
                   }


这篇关于在Mozilla Firefox中使用javascript的日期比较不起作用?Plz帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆