Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function below calculates day of the week for any given date. not returning correctly, plz help

Status
Not open for further replies.

obtur

Programmer
Jul 16, 2020
2
AU
The function below calculates the day of the week for any given date. It currently is not returning the correct day.

The function is not working correctly. Fix it to return the correct day. <strong>Test cases: 26/07/1969 = Saturday

Also please update the function to print the day and month as follows.
26/07/1969 = Saturday, 26 July 1969 || 31/12/1999 = Friday, 31 December 1999.

thank you so very much
 
 https://files.engineering.com/getfile.aspx?folder=0c48de38-e84d-45fd-8422-aa8db26f3fc9&file=test_day.html
simple if you step through your code and / or use any of the existing functions on the web that will do this for you without reinventing the wheel. Is this a homework assignment ?

JavaScript:
function getDayName() {

    var myDate= document.getElementById("dateInput").value;
	var days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
	var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

	if(myDate == ""){
		document.getElementById("result").innerHTML = "Invalid date. Please enter a date in dd/mm/yyyy fomat"
	}
	else {
		document.getElementById("result").innerHTML = "";
		myDate=myDate.split("/");
		var newDate=myDate[2]+"-"+myDate[1]+"-"+myDate[0];
		var currentDate = new Date(newDate);
		var day_name = days[currentDate.getDay()];
		var month_name = months[currentDate.getMonth()];
		var year = myDate[2];

		document.getElementById("result").innerHTML = day_name + "," + currentDate.getDay() + " " + month_name + " " + myDate[2];
	}
}


Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Thank you so very much :) Yes it was.... :-D

I did it like this below :


function getDayName() {

var myDate= document.getElementById("dateInput").value;

if(myDate == ""){
document.getElementById("result").innerHTML = "Invalid date. Please enter a date in dd/mm/yyyy fomat"
}
else {
document.getElementById("result").innerHTML = "";
myDate=myDate.split("/");
var newDate=myDate[2]+"/"+myDate[1] +"/"+myDate[0];
var currentDate = new Date(newDate);

var day_name = currentDate.getDay();
var day = currentDate.getDate();
//var year = currentdate.getFullYear();

var days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var month_name = currentDate.getMonth();
var year = myDate[2];

document.getElementById("result").innerHTML = days[day_name] + ", " + day + " " + months[month_name] + " " + year;
}
}


Thanks huge again....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top