• Status: Solved
• Priority: Medium
• Security: Public
• Views: 404

# find the given date is Sunday?

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
arogyarajxavier
• 4
• 2
• 2
• +1
1 Solution

Commented:
Try this example :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<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()
}else{
alert("invalid date. Enter in mm/dd/yyyy format");
}
}

//-->
</SCRIPT>

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

</form>

</BODY>
</HTML>
0

Author Commented:
Thank you nimaig. no validation required. i couldnot get the result using ur script.
0

Commented:
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();

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

Commented:
0

Commented:
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>
<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

Commented:
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>

<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

Commented:
Hmmm... Did I answer the wrong question c\:-/
0

Commented:
<Script>
var gd=new Date(2001,5,13)
if(gd.getDay()==1)
else
</script>
0

Author Commented:
thanks nimaig for immeditate effort.
thank you nilapenn and russellshome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.