0

我有一个字符串格式的日期范围,例如 12/20/12-12/24/12(start date-end date)。我需要检查开始日期是否已超过当前日期。可以使用 JS 或 j-query 来完成吗?日期范围是字符串?

4

3 回答 3

3

使用Date物体的力量!

var myString = "12/20/2012-12/24/2012",
    parts = myString.split("-");
if (Date.parse(parts[0]) < Date.now()) {
    alert("start date has past the present date");
}

你也可以写new Date(parts[0]) < new Date

于 2012-12-12T16:08:13.627 回答
1

使用datejs

var today = Date.today();
var yourDay = Date.parse(yourDateTimeString);
var result = Date.compare(today, yourDay);
var isValid= result == -1;

comparetoday如果小于则返回 -1yourDay

于 2012-12-12T16:13:19.270 回答
0

thanx evry1 ..我在这里得到了答案...

 function cancel()
{
        var eventId=status_ID.substr(0,status_ID.indexOf("-"));
        var status=status_ID.substr(status_ID.indexOf("-")+1,status_ID.indexOf(":")-4);
        var eventRange=status_ID.substr(status_ID.indexOf(":")+1,status_ID.length);


        //alert(status);
        //alert(eventRange);
        if(status=="OPEN"){

            var startDate = eventRange.substr(0,eventRange.indexOf("-"));

           //alert(startDate);
          // alert("open");
           var todayDay = new Date().getDate();
          // alert(todayDay);
           var todayMonth = new Date().getMonth();
           alert(todayMonth);
           var todayFullYear = new Date().getFullYear();

           var todayYear=todayFullYear.toString().substr(2,4);
          // alert(todayYear);

            parts = startDate.split("/");
           //alert(parts[0]);
           //alert(parts[1]);
           //alert(parts[2]);
           var flag1=false;
           if(parts[2]>todayYear){
               flag1=true;
           }
           if(parts[2]==todayYear){
               if(parts[1]>todayMonth+1)
               {
                   flag1=true;
               }
               if(parts[1]==todayMonth+1)
               {
                   if(parts[0]>todayDay)
                   {
                       flag1=true;
                   }
               }

           }
           if(flag1)
           {
               alert("event can be cancelled");

                document.forms[0].submit();
           }



           else
           {
               alert("event has already started");
               return false;
           }


          }
        else
        {
            alert("The event has closed");
        }

    }
于 2012-12-13T11:20:29.807 回答