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

Recommended Answers

All 5 Replies

Test drive this one! It's old and dusty, but works on Dev-C++ version 4.9.9.0

// 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);
}

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"?
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.

Works great guys, thx! This is pretty cool stuff ^^

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 write a program that accepts a date from the user in the form mm/dd/yy and then display it in the form yymmdd:
it is in the format:
Enter a date (mm/dd/yy): 08/31/10
You have entered the date: 100831

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.