Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

find the given date is Sunday?

Posted on 2001-06-13
9
391 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
ID: 6184982
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
ID: 6185079
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
ID: 6185185
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:russellshome
ID: 6185384
0
 
LVL 5

Expert Comment

by:russellshome
ID: 6185394
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
ID: 6185399
              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
ID: 6185413
Hmmm... Did I answer the wrong question c\:-/
0
 
LVL 5

Expert Comment

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

Author Comment

by:arogyarajxavier
ID: 6189114
thanks nimaig for immeditate effort.
thank you nilapenn and russellshome.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

861 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