Solved

find the given date is Sunday?

Posted on 2001-06-13
9
394 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
[X]
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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

623 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