Advertisement
Divyansh_Chourey

date_difference

Mar 10th, 2024
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | Source Code | 0 0
  1. #include<stdio.h>
  2.  
  3. int leap_year(int year);
  4. //checks if a year is leap year or not
  5.  
  6. int days_in_month(int month_num);
  7. //takes month number and returns number of days
  8.  
  9. int date_to_days(int date, int month, int year);
  10. //takes date and calculate number of days from reference date
  11.  
  12. int days_to_date(int days);
  13. //takes days and convert to date format
  14.  
  15. int main(){
  16.     int year1, year2, month1, month2, date1, date2, result;
  17.     printf("\nEnter valid date(dd mm yyyy)\nFirst date must be smaller than second date\n");
  18.     printf("Enter first date: ");
  19.     scanf("%d %d %d", &date1, &month1, &year1);
  20.     printf("Enter second date: ");
  21.     scanf("%d %d %d", &date2, &month2, &year2);
  22.     if(date1>31 || date2>31 || month1>12 || month2>12){
  23.         printf("Invalid date");
  24.     }
  25.     else{
  26.         result = date_to_days(date2, month2, year2)-date_to_days(date1, month1, year1);
  27.         printf("total number of days: %d\n", result);
  28.         days_to_date(result);
  29.     }
  30.     return 0;
  31. }
  32.  
  33. int leap_year(int year){
  34.     if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
  35.         return 1;
  36.     }
  37.     else{
  38.         return 0;
  39.     }
  40. }
  41.  
  42. int days_in_month(int month_num){
  43.     int days=0, year=2018;
  44.     if(month_num==1 ||month_num==3 ||month_num==5 ||month_num==7 ||month_num==8 ||month_num==10 ||month_num==12 ){
  45.         return days+=31;
  46.     }
  47.     else if(month_num==4 ||month_num==6 ||month_num==9 ||month_num==11 ){
  48.         return days+=30;
  49.     }
  50.     else if(month_num==2 && leap_year(year)){
  51.         return days+=29;
  52.     }
  53.     else{
  54.         return days+=28;
  55.     }
  56. }
  57.  
  58. int date_to_days(int date, int month, int year){
  59.     int reference = 1500, result;
  60.     int a=reference, b=(year-1), days=0;
  61.     for(int i = a; i<=b; i++){
  62.         if (leap_year(i)){
  63.             days += 366;
  64.         }
  65.         else{
  66.             days += 365;
  67.         }
  68.     }
  69.     for(int i=1; i<=month; i++){
  70.         days+=days_in_month(i);
  71.     }
  72.     days+=date;
  73.     return days;
  74.    
  75. }
  76.  
  77. int days_to_date(int days){
  78.     int year=0, month=0, date=0;
  79.     if (days>=365){
  80.         year = days/365;
  81.         days-=year*365;
  82.     }
  83.     if (days>=30){
  84.         month = days/30;
  85.         days-=month*30;
  86.     }
  87.     printf("%d years, %d months, %d days", year, month, days);
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement