Solved

find the given date is Sunday?

Posted on 2001-06-13
9
388 Views
Last Modified: 2007-11-27
I am getting a date value in the format of dd/mm/yyyy. I want to find the given date is Sunday or not. thanks
0
Comment
Question by:arogyarajxavier
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 9

Expert Comment

by:nimaig
Comment Utility
Try this example :


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>

<SCRIPT LANGUAGE="JavaScript">
<!--
       
var days = new Array(" Sunday ", " Monday ", " Tuesday "," Wednesday ", " Thursday ", " Friday ", " Saturday ");
         
function StringToDate(dateString){
     var err = 0

     var valid = "0123456789/"
     var ok = "yes";
     var temp;
     if(trim(dateString)!="")
     {
          for (var i=0; i< dateString.length; i++)
          {
               temp = "" + dateString.substring(i, i+1);
               if (valid.indexOf(temp) == "-1") err = 1;
          }
          if (dateString.length != 10) err=1
          b = dateString.substring(0, 2) // month
          c = dateString.substring(2, 3)// '/'
          d = dateString.substring(3, 5) // day
          e = dateString.substring(5, 6)// '/'
          f =    dateString.substring(6, 10) // year
      if (b<1 || b>12) err = 1
          if (c != '/') err = 1
          if (d<1 || d>31) err = 1
          if (e != '/') err = 1
          if (f<0 || f>3000) err = 1
          if (b==4 || b==6 || b==9 || b==11)
          {
            if (d==31) err=1
          }
          if (b==2)
          {
           var g=parseInt(f/4)
            if    (isNaN(g))
            {
                err=1
            }
            if (d>29) err=1
            if (d==29 && ((f/4)!=parseInt(f/4))) err=1
          }
          if (err==1)
          {
               return "-1" ;
          }
          else
          {
            return new Date(f,b-1,d);
          }
     }
     else
     {
          return "-1";
     }

}

          function RTrim(str){
                var whitespace = new String(" \t\n\r");
                var s = new String(str);
                if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
                    var i = s.length - 1;      
                    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                        i--;
                    s = s.substring(0, i+1);
                }

                return s;
        }

        function LTrim(str){
                var whitespace = new String(" \t\n\r");
                var s = new String(str);
                if (whitespace.indexOf(s.charAt(0)) != -1) {
                    var j=0, i = s.length;
                    while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                        j++;
                    s = s.substring(j, i);
                }
                return s;
        }
         
        function trim(str){
     
                return RTrim(LTrim(str));
        }




function getDay(){
     var myDate=document.forms[0].txtDate.value;
     var myDateObj = StringToDate(myDate);
     if(myDateObj != -1) {
          var weekday;
          weekday = myDateObj.getDay()
          alert(days[weekday]);
     }else{
          alert("invalid date. Enter in mm/dd/yyyy format");
     }
}
         
         
//-->
</SCRIPT>
</HEAD>

<BODY>
<form>
     <input type="text" name="txtDate">(mm/dd/yyyy)
     <input type="button" name="button1" value="getDay" onClick="getDay()">
     
</form>

</BODY>
</HTML>
0
 

Author Comment

by:arogyarajxavier
Comment Utility
Thank you nimaig. no validation required. i couldnot get the result using ur script.
0
 
LVL 9

Accepted Solution

by:
nimaig earned 10 total points
Comment Utility
enter 06/13/2001 in the input box and press the command button. You would get an alert box saying "wednesday"

All the functions like trim(),ltrim(), rtrim() and StringToDate() are for validation. If you don't require don't use it.

try this :
var myDate = new Date();
alert(myDate.getDay())

and it displays '3' means wednesday. Check if the value obtained is '0'. If it is '0' the day is sunday
0
 
LVL 5

Expert Comment

by:russellshome
Comment Utility
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 5

Expert Comment

by:russellshome
Comment Utility
Hmmm my code example was too long.

My idea is to use selectors rather than an input box to enter the date. We in NZ use DD/MM/YY for dates so 01/12/01 is 01 Dec but to others it is 12 Jan.

Here is part 1 of my example:

<html>
     <head>
          <title>Date Test</title>
          <script>
               function monthName(monthNumber) {
                    switch(monthNumber) {
                         case 0:
                              return "Jan"
                              break;
                         case 1:
                              return "Feb"
                              break;
                         case 2:
                              return "Mar"
                              break;
                         case 3:
                              return "Apr"
                              break;
                         case 4:
                              return "May"
                              break;
                         case 5:
                              return "Jun"
                              break;
                         case 6:
                              return "Jul"
                              break;
                         case 7:
                              return "Aug"
                              break;
                         case 8:
                              return "Sep"
                              break;
                         case 9:
                              return "Oct"
                              break;
                         case 10:
                              return "Nov"
                              break;
                         case 11:
                              return "Dec"
                              break;
                    }
               }
               
               function getNumberOptions(selectObj, start, end, defaultValue) {
                    var numOptions = end - start + 1
                    selectObj.length = numOptions
                    for (var i=0;i<numOptions;i++) {
                         var theValue = i+start
                         var theValueString = "" + theValue
                         if ((theValue <= 9) && (theValue >= -9)) {
                              theValueString = "0" + theValueString
                         }
                         selectObj.options[i] = new Option(theValueString)
                    }
                    selectObj.options[defaultValue - start].selected = true
               }
0
 
LVL 5

Expert Comment

by:russellshome
Comment Utility
              function init() {
                    var f = document.dateForm
                    var today = new Date()
     
                    var theDay = today.getDate()
                    getNumberOptions(f.day, 1, 31, theDay);
     
                    f.currentDay.value = f.day.selectedIndex
     
                    f.month.length = 12
                    for (var i=0;i<12;i++) {
                         f.month.options[i].text = monthName(i)
                         f.month.options[i].value = i
                    }
                    f.month.options[today.getMonth()].selected = true
               
                    var theCentury = Math.floor(today.getFullYear() / 100)
                    getNumberOptions(f.century, theCentury - 1, theCentury + 1, theCentury);
                   
                    var theYear = today.getFullYear() - Math.floor(today.getFullYear() / 100) * 100
                    getNumberOptions(f.year, 0, 99, theYear);
                   
               }
               
               function checkDate() {
                    var f = document.dateForm
                 var dObj = f.day
                    var mObj = f.month
                    var cObj = f.century
                    var yObj = f.year
                    var dateString = ""
                    dateString += mObj.options[mObj.selectedIndex].text + " "
                    dateString += dObj.options[dObj.selectedIndex].text + ", "
                    dateString += cObj.options[cObj.selectedIndex].text
                    dateString += yObj.options[yObj.selectedIndex].text
                   
                    // Check whether the parsed day is different to selected day
                    var testDate = new Date(dateString)
                    if (dObj.options[dObj.selectedIndex].text != testDate.getDate()) {
                         alert(dateString + " is an invalid date")
                         dObj.selectedIndex = document.dateForm.currentDay.value
                    }
                    f.currentDay.value=dObj.selectedIndex
               }
          </script>
     </head>
     
     <body onLoad="init()">
     
     <form name="dateForm">
          <input type="hidden" name="currentDay">
          <table>
               <tr>
                    <td>
                         <select name="day" onChange="checkDate()">
                              <option name="dummy" value="0">0</option>
                         </select>
                    </td>
                    <td>
                         <select name="month">
                              <option name="dummy" value="0">0</option>
                         </select>
                    </td>
                    <td>
                         <select name="century">
                              <option name="dummy" value="0">0</option>
                         </select>
                    </td>
                    <td>
                         <select name="year">
                              <option name="dummy" value="0">0</option>
                         </select>
                    </td>
               </tr>
          </table>
     </form>
     
     </body>
</html>
0
 
LVL 5

Expert Comment

by:russellshome
Comment Utility
Hmmm... Did I answer the wrong question c\:-/
0
 
LVL 5

Expert Comment

by:nilapenn
Comment Utility
<Script>
var gd=new Date(2001,5,13)
if(gd.getDay()==1)
alert('Sunday')
else
alert('Not Sunday')
</script>
0
 

Author Comment

by:arogyarajxavier
Comment Utility
thanks nimaig for immeditate effort.
thank you nilapenn and russellshome.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now