| | |
Simple Time/Date Program
![]() |
•
•
Join Date: Oct 2004
Posts: 11
Reputation:
Solved Threads: 0
Hello Everyone,
I've been desperatly trying to teach myself C and I've come along quite nicely. I wanted to start making my own programs (not from internet tutorial/Cd Rom).
I want to make a program that will display two dates that the user inputs then calculate the number of days between those two dates. Then once I do that I can go onto make days/weeks/months/years between the two dates. However, I don't really know how to start this problem. I've worked on it for a while but nothing is coming together. Could someone kindly show me how I would write this problem or at least tell me how to get started. I'm really looking forward to making this work! ^^ thx
I've been desperatly trying to teach myself C and I've come along quite nicely. I wanted to start making my own programs (not from internet tutorial/Cd Rom).
I want to make a program that will display two dates that the user inputs then calculate the number of days between those two dates. Then once I do that I can go onto make days/weeks/months/years between the two dates. However, I don't really know how to start this problem. I've worked on it for a while but nothing is coming together. Could someone kindly show me how I would write this problem or at least tell me how to get started. I'm really looking forward to making this work! ^^ thx
Test drive this one! It's old and dusty, but works on Dev-C++ version 4.9.9.0
[php]// Find number of days between dates
// Turbo C converted to run on Dev-Cpp (vegaseat)
#include <iostream>
#include <stdlib.h>
using namespace std;
long days(int sm, int sd, int sy, int em, int ed, int ey);
long factor(int mm, int dd, int yy);
int main(int argc, char *argv[])
{
int sm, sd, sy; // starting date
int em, ed ,ey; // ending date
long old;
printf("\n Calculate days between two dates .....");
printf("\n\n Enter first date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&sm,&sd,&sy);
printf("\n Enter second date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&em,&ed,&ey);
old = days(sm,sd,sy,em,ed,ey);
printf("\n There are %ld days between %d/%d/%d and %d/%d/%d\n",
old,sm,sd,sy,em,ed,ey);
system("PAUSE");
return 0;
}
long days(int sm, int sd, int sy, int em, int ed, int ey)
{
long fac1, fac2, elap;
fac1 = factor(sm, sd, sy);
fac2 = factor(em, ed, ey);
elap = fac2 - fac1;
return (elap);
}
long factor(int mm, int dd, int yy)
{
long xx;
xx = 365 * yy + dd + 31 * (mm - 1);
if (mm < 3)
xx = xx + ((yy -1)/4) - (.75 * ((yy - 1)/100)+1);
else
xx = xx - (.4 * mm + 2.3) + (yy / 4) - (((.75 * (yy / 100) + 1)));
return (xx);
}
[/php]
[php]// Find number of days between dates
// Turbo C converted to run on Dev-Cpp (vegaseat)
#include <iostream>
#include <stdlib.h>
using namespace std;
long days(int sm, int sd, int sy, int em, int ed, int ey);
long factor(int mm, int dd, int yy);
int main(int argc, char *argv[])
{
int sm, sd, sy; // starting date
int em, ed ,ey; // ending date
long old;
printf("\n Calculate days between two dates .....");
printf("\n\n Enter first date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&sm,&sd,&sy);
printf("\n Enter second date in mm/dd/yyyy format: ");
scanf("%d/%d/%d",&em,&ed,&ey);
old = days(sm,sd,sy,em,ed,ey);
printf("\n There are %ld days between %d/%d/%d and %d/%d/%d\n",
old,sm,sd,sy,em,ed,ey);
system("PAUSE");
return 0;
}
long days(int sm, int sd, int sy, int em, int ed, int ey)
{
long fac1, fac2, elap;
fac1 = factor(sm, sd, sy);
fac2 = factor(em, ed, ey);
elap = fac2 - fac1;
return (elap);
}
long factor(int mm, int dd, int yy)
{
long xx;
xx = 365 * yy + dd + 31 * (mm - 1);
if (mm < 3)
xx = xx + ((yy -1)/4) - (.75 * ((yy - 1)/100)+1);
else
xx = xx - (.4 * mm + 2.3) + (yy / 4) - (((.75 * (yy / 100) + 1)));
return (xx);
}
[/php]
May 'the Google' be with you!
•
•
Join Date: Oct 2004
Posts: 11
Reputation:
Solved Threads: 0
Thx a bunch! Some quick question though.
1. What are all the "long" functions for? Do I have to use "long"?
And what is the "old" refering to?
2. (just trying to understand this program) what are your "e"s and "s"s?
ex: em, ed, ey
3. What does elap stand for?
4. I understand this code for the most part but I only have a free unix system I got off the internet, can I use this code on that?
thx again
1. What are all the "long" functions for? Do I have to use "long"?
And what is the "old" refering to?
2. (just trying to understand this program) what are your "e"s and "s"s?
ex: em, ed, ey
3. What does elap stand for?
4. I understand this code for the most part but I only have a free unix system I got off the internet, can I use this code on that?
thx again
>1. What are all the "long" functions for? Do I have to use "long"?
They're for the date and time calculations. You don't have to use long, but it helps to avoid integer overflow.
>2. (just trying to understand this program) what are your "e"s and "s"s?
Poor naming. s is a prefix meaning start (ex. sy == start year), and e is a prefix meaning end.
>3. What does elap stand for?
Time elapsed.
>can I use this code on that?
Provided it's fixed, yes. At present it relies on the implementation of iostream including stdio.h, not to mention also assuming that this code is C++ and not C. Replace <iostream> with <stdio.h> and remove "using namespace std;" and the code should compile under any C implementation.
They're for the date and time calculations. You don't have to use long, but it helps to avoid integer overflow.
>2. (just trying to understand this program) what are your "e"s and "s"s?
Poor naming. s is a prefix meaning start (ex. sy == start year), and e is a prefix meaning end.
>3. What does elap stand for?
Time elapsed.
>can I use this code on that?
Provided it's fixed, yes. At present it relies on the implementation of iostream including stdio.h, not to mention also assuming that this code is C++ and not C. Replace <iostream> with <stdio.h> and remove "using namespace std;" and the code should compile under any C implementation.
I'm here to prove you wrong.
![]() |
Similar Threads
- I just need TIME not Date. (MS SQL)
- Code Snippet: Time and Date Display (C++)
- Using the time and date? (C++)
- Time display program (C++)
- Help with a Time Keeping Program (Computer Science)
Other Threads in the C Forum
- Previous Thread: code subtlety
- Next Thread: Is there a way to conver .exe back to .cpp
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue number odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






