Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,145 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,081 people online right now. Registration is fast and FREE... Join Now!




how to figure the day of week from a user given date

 
Reply to this topicStart new topic

how to figure the day of week from a user given date, day of week calculator: doomsday calculator

new2programs
10 Apr, 2007 - 01:45 PM
Post #1

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 29


My Contributions
Hi,

I need to write a program to convert a user given input date say 15/3/2006 and find out what day of the week it was. I tried to code but I am still facing difficulties.
lets say there can be any dates between 2005 and 2020.I am not being able to figure how to convert teh date.Could someone please help????
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: How To Figure The Day Of Week From A User Given Date
10 Apr, 2007 - 02:17 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,984



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
System.DateTime date = new System.DateTime(year,month,day);
String day_of_week = date.DayOfWeek;

the upper bound i am not sure of, but this is guarenteed at least as far back as 1753
User is offlineProfile CardPM
+Quote Post

new2programs
RE: How To Figure The Day Of Week From A User Given Date
10 Apr, 2007 - 02:35 PM
Post #3

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 29


My Contributions
QUOTE(William_Wilson @ 10 Apr, 2007 - 03:17 PM) *

System.DateTime date = new System.DateTime(year,month,day);
String day_of_week = date.DayOfWeek;

the upper bound i am not sure of, but this is guarenteed at least as far back as 1753



No, I have to do this by using only for loos, while loops etc.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: How To Figure The Day Of Week From A User Given Date
10 Apr, 2007 - 02:47 PM
Post #4

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,984



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
well you could figure out todays value, using System.DateTime.Now() then using an array of the days of the week. Then figure out the difference in days from today and the day entered, modulo it to get the remainder and add that to the value of today in the array, (watching to cycle around should the value be larger than 6, since the array will be 0-6 values) and this should be the day.
User is offlineProfile CardPM
+Quote Post

salmalita
RE: How To Figure The Day Of Week From A User Given Date
10 Apr, 2007 - 06:45 PM
Post #5

New D.I.C Head
*

Joined: 25 Mar, 2007
Posts: 8


My Contributions
QUOTE(new2programs @ 10 Apr, 2007 - 02:45 PM) *

Hi,

I need to write a program to convert a user given input date say 15/3/2006 and find out what day of the week it was. I tried to code but I am still facing difficulties.
lets say there can be any dates between 2005 and 2020.I am not being able to figure how to convert teh date.Could someone please help????

if i understand what u mean exactly i think this is the code;
CODE

#include<stdio.h>
    int leap (int year);

        int main (void){
            int month,day,year,dm,dn,leap;
            
             printf("enter the month:");
      scanf("%d",&month);
      
      printf("enter the day:");
      scanf("%d",&day);
      
      printf("enter the year:");
      scanf("%d",&year);
        
    if (leap==0)
           {      if(month==1)
                               dm=0;
              if(month==2)
                              dm=31;  

              if(month==3)
                           dm=59;
              if(month==4)
                           dm=90;
              if(month==5)
                           dm=120;
              if(month==6)
                           dm=151;
              if(month==7)
                           dm=181;
              if(month==8)
                           dm=212;
              if(month==9)
                           dm=243;
              if(month==10)
                           dm=273;
              if(month==11)
                           dm=304;
              if(month==12)
                           dm=334;}
                                      
     else    
     {          if(month==1)
                               dm=0;
              if(month==2)
                              dm=31;                        
            
              if(month==3)
                           dm=60;
              if(month==4)
                           dm=91;
              if(month==5)
                           dm=121;
              if(month==6)
                           dm=152;
              if(month==7)
                           dm=182;  
              if(month==8)
                           dm=213;
              if(month==9)
                           dm=244;
              if(month==10)
                           dm=274;
              if(month==11)
                           dm=304;
              if(month==12)
                           dm=335;}  
      
        dn=dm+day;
        
      printf("the day number is :%d",dn);  
      
return 0;
}                  
    
    int leap (int year){
        if((year%100== 0 && year%400==0)||  (year%4==0))
        
                return 1;
            else
                return 0;}

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: How To Figure The Day Of Week From A User Given Date
10 Apr, 2007 - 10:46 PM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
This is called the doomsday algorithm and you can find a nice little discription of it on wikipedia.
An example in C++
User is offlineProfile CardPM
+Quote Post

new2programs
RE: How To Figure The Day Of Week From A User Given Date
11 Apr, 2007 - 09:24 AM
Post #7

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 29


My Contributions
Thanks,

Just one question., this gives me the day number, so if i want the weekday(like sunday, monday), taking the remainder of dn%7 should give me the weekday (if sunday=0,monday=1)right?


QUOTE(salmalita @ 10 Apr, 2007 - 07:45 PM) *

QUOTE(new2programs @ 10 Apr, 2007 - 02:45 PM) *

Hi,

I need to write a program to convert a user given input date say 15/3/2006 and find out what day of the week it was. I tried to code but I am still facing difficulties.
lets say there can be any dates between 2005 and 2020.I am not being able to figure how to convert teh date.Could someone please help????

if i understand what u mean exactly i think this is the code;
CODE

#include<stdio.h>
    int leap (int year);

        int main (void){
            int month,day,year,dm,dn,leap;
            
             printf("enter the month:");
      scanf("%d",&month);
      
      printf("enter the day:");
      scanf("%d",&day);
      
      printf("enter the year:");
      scanf("%d",&year);
        
    if (leap==0)
           {      if(month==1)
                               dm=0;
              if(month==2)
                              dm=31;  

              if(month==3)
                           dm=59;
              if(month==4)
                           dm=90;
              if(month==5)
                           dm=120;
              if(month==6)
                           dm=151;
              if(month==7)
                           dm=181;
              if(month==8)
                           dm=212;
              if(month==9)
                           dm=243;
              if(month==10)
                           dm=273;
              if(month==11)
                           dm=304;
              if(month==12)
                           dm=334;}
                                      
     else    
     {          if(month==1)
                               dm=0;
              if(month==2)
                              dm=31;                        
            
              if(month==3)
                           dm=60;
              if(month==4)
                           dm=91;
              if(month==5)
                           dm=121;
              if(month==6)
                           dm=152;
              if(month==7)
                           dm=182;  
              if(month==8)
                           dm=213;
              if(month==9)
                           dm=244;
              if(month==10)
                           dm=274;
              if(month==11)
                           dm=304;
              if(month==12)
                           dm=335;}  
      
        dn=dm+day;
        
      printf("the day number is :%d",dn);  
      
return 0;
}                  
    
    int leap (int year){
        if((year%100== 0 && year%400==0)||  (year%4==0))
        
                return 1;
            else
                return 0;}



User is offlineProfile CardPM
+Quote Post

new2programs
RE: How To Figure The Day Of Week From A User Given Date
11 Apr, 2007 - 11:31 AM
Post #8

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 29


My Contributions
hi,

it still doesnt give me the day.........
User is offlineProfile CardPM
+Quote Post

new2programs
RE: How To Figure The Day Of Week From A User Given Date
11 Apr, 2007 - 05:47 PM
Post #9

New D.I.C Head
*

Joined: 10 Apr, 2007
Posts: 29


My Contributions
I got it! Thanks!
User is offlineProfile CardPM
+Quote Post

TwoSpots
RE: How To Figure The Day Of Week From A User Given Date
11 Apr, 2007 - 09:10 PM
Post #10

New D.I.C Head
*

Joined: 2 Feb, 2007
Posts: 45


My Contributions
QUOTE(new2programs @ 11 Apr, 2007 - 12:31 PM) *

hi,

it still doesnt give me the day.........


You could also get the user to enter the date as a string(mn/dy/yr) then split it up. i.e.
CODE



Date(const string &data)
{
    // determine month
    stringstream buffer(data.substr(0,2));
    int mn = 0;
    buffer >> mn;

    // determine day
    stringstream buffer2(data.substr(3,5));
    int dy = 0;
    buffer2 >> dy;

    // determine year
    stringstream buffer3(data.substr(6,8));
    int yr = 0;
    buffer3 >> yr;

    // convert year into a string
    stringstream ss;
    ss << yr;
    ss.str();

    string result =  ss.str();

    // determine the lenght of the string
    string::size_type len;
    len = result.size();

        switch(len)
        {
            case 0:
            result.insert(0,"2000");
            break;
            case 1:
            result.insert(0,"200");
            break;
            case 2:
            result.insert(0,"20");
            break;
        }


    // convert the string back to an integer
    stringstream buffer4(result);
    int normalYr = 0;
    buffer4 >> normalYr;

    if(mn <= 0 && mn > 12) // validate the month
    {
        month = 1; // invalid month set to 1
        cout << "Invalid month (" << month << ") set to 1.\n";
    } // end else
    else
    {
    month = mn;
    }
    year  = normalYr;
    day  = checkDay(dy); // validate the day and check for a leap year
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:52PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month