Important alert: (current site time 11/26/2014 10:40:54 AM EDT)
 

VB icon

Calculate day in a week by given a date

Email
Submitted on: 11/14/2001 10:38:36 AM
By: Stanley Wong 
Level: Beginner
User Rating: By 4 Users
Compatibility: C, C++ (general), Microsoft Visual C++
Views: 27179
(About the author)
 
     This program can calculate the day of the week from a date key in by user.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :Calculate day in a week by given a date
//**************************************
stdio.h
string.h
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Calculate day in a week by given a date
// Description:This program can calculate the day of the week from a date key in by user.
// By: Stanley Wong
//
// Inputs:a date
//
// Returns:day of week
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2857&lngWId=3//for details.//**************************************

#include <stdio.h>
#include <string.h>
/* Prototype Declaration */
int validateDate(int dd, int mm, int yyyy);
void printError();
int calcDay_Dec31(int yyyy);
int dayInYear(int dd, int mm);
void nameInStr (char daysInWord[], int days);
void main(void)
{
	int dd, mm, yyyy;
	int days;
	char daysInWord[11];
	
	/* Read a date and validate the date */
	do{
		printf("Enter a date(dd/mm/yyyy) :");
		scanf("%d / %d / %d", &dd, &mm ,&yyyy);
		fflush(stdin);
	}
	
	while(validateDate(dd, mm, yyyy));
	
	/* Calculate the day for Dec 31 of the previous year */
	days = calcDay_Dec31(yyyy);
	/* Calculate the day for the given date */
	days = (dayInYear(dd, mm) + days) % 7;
	/* Add one day if the year is leap year and desired date is after February */
	if ((!(yyyy % 4) && (yyyy % 100) || !(yyyy % 400)) && mm > 2)
		days++;
	nameInStr(daysInWord, days);
	/* Print the day of the desired date */
	printf("The day for date %d/%d/%d is %s\n\n", dd, mm, yyyy, daysInWord);
} /* main */
int validateDate(int dd, int mm, int yyyy)
{
	int i = 0, j = 0;
	int a[7] = {1, 3, 5, 7, 8, 10, 12};
	int b[4] = {4, 6, 9, 11};
	int error = 0;
	if (mm < 1 || mm > 12)
		error = 1;
	
	
	if (mm == 2)
	{
		if (!(yyyy % 4) && (yyyy % 100) || !(yyyy % 400))
		{
			if (dd < 1 || dd > 29)
				error = 1;
							
		}
		else if (dd < 1 || dd >28)
			error = 1;
	}
 	
	for (i=0;i<6;i+=1)
	{	
		if (mm == a[i])
		{
			if (dd < 1 || dd > 31)
				error = 1;
		}
	}
	for (j=0;j<4;j+=1)
	{
		if (mm == b[j])
		{
			if (dd < 1 || dd > 30)
				error = 1;
		}
	}
	if (error == 1)
		printError();
	return error;
}
void printError()
{
	printf("Invalid Input!\n\n");
}
int calcDay_Dec31(int yyyy)
{
	int dayCode = 0;
	dayCode = ((yyyy-1)*365 + (yyyy-1)/4 - (yyyy-1)/100 + (yyyy-1)/400) % 7;
	return dayCode;
} /* calcDay_Dec31 */
int dayInYear(int dd, int mm)
{
	switch(mm)
	{
	case 12:dd += 30;
	case 11:dd += 31;
	case 10:dd += 30;
	case 9:dd += 31;
	case 8:dd += 31;
	case 7:dd += 30;
	case 6:dd += 31;
	case 5:dd += 30;
	case 4:dd += 31;
	case 3:dd += 28;
	case 2:dd += 31;
	}
	return dd;
} /* dayInYear */
void nameInStr(char daysInWord[], int days)
{
	switch(days)
	{
	case 0:strcpy(daysInWord, "Sunday");break;
	case 1:strcpy(daysInWord, "Monday");break;
	case 2:strcpy(daysInWord, "Tuesday");break;
	case 3:strcpy(daysInWord, "Wednesday");break;
	case 4:strcpy(daysInWord, "Thursday");break;
	case 5:strcpy(daysInWord, "Friday");break;
	case 6:strcpy(daysInWord, "Saturday");break;
	}
} /* nameInStr */


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/15/2001 3:35:43 AMMatthew Thomson

Nice little piece of code that!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.