user1643087
user1643087

Reputation: 643

check if the given date(in string format) has past present date using JS

I have a date range like 12/20/12-12/24/12(start date-end date) in string format . I need to check if start date has past the present date. Can it be done using JS or j-query? the date range is in string ?

Upvotes: 0

Views: 3896

Answers (3)

user1643087
user1643087

Reputation: 643

thanx evry1 .. i got the answer here it is ...

 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");
        }

    }

Upvotes: 0

lante
lante

Reputation: 7336

using datejs

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

compare returns -1 if today is less than yourDay

Upvotes: 1

Bergi
Bergi

Reputation: 664434

Use the power of the Date object!

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");
}

You could as well write new Date(parts[0]) < new Date.

Upvotes: 3

Related Questions