#include<stdio.h>
main()
{
typedef struct{int dd,mm,yy;}tanggal;
tanggal d1,d2;
int i,hari,hari1,selisih,selisih1,total;
int jumlah=0,jumlah1=0,tahun,hari2=0;
printf("masukkan tanggal pertama (dd/mm/yyyy): "); scanf("%d-%d-%d",&d1.dd,&d1.mm,&d1.yy);
printf("masukkan tanggal kedua (dd/mm/yyyy): "); scanf("%d-%d-%d",&d2.dd,&d2.mm,&d2.yy);
if(d2.yy>d1.yy)
{
for(i=12;i>=d1.mm;i--)
{
switch(i)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
hari=31;break;
case 4: case 6: case 9: case 11:
hari=30;break;
case 2:
if((d1.yy%4==0&&d1.yy%100!=0)||(d1.yy%400==0))
hari=29;
else
hari=28;
}
jumlah=jumlah+hari;
}
selisih=jumlah-d1.dd;
for(i=1;i<=d2.mm-1;i++)
{
switch(i)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
hari1=31;break;
case 4: case 6: case 9: case 11:
hari1=30;break;
case 2:
if((d2.yy%4==0&&d2.yy%100!=0)||(d2.yy%400==0))
hari1=29;
else
hari1=28;
}
jumlah1=jumlah1+hari1;
}
selisih1=jumlah1+d2.dd;
for(i=d1.yy+1;i<d2.yy;i++)
{
if((i%4==0&&i%100!=0)||(i%400==0))
tahun=366;
else
tahun=365;
hari2=hari2+tahun;
}
total=selisih+selisih1+hari2;
printf("Selisih hari adalah= %d",total);
}
else if(d1.yy==d2.yy&&d2.mm>d1.mm)
{
for(i=d1.mm;i<=d2.mm-1;i++)
{
switch(i)
{
case 1: case 3 : case 5: case 7: case 8: case 10: case 12:
hari=31;break;
case 4: case 6: case 9: case 11:
hari=30;break;
case 2:
if((d1.yy%4==0&&d1.yy%100!=0)||(d1.yy%400==0))
hari=29;
else
hari=28;
}
jumlah=jumlah+hari;
}
selisih=(jumlah+d2.dd)-d1.dd;
printf("Selisih hari adalah= %d",selisih);
}
else if(d1.yy==d2.yy&&d1.mm==d2.mm&&d2.dd>d1.dd)
{
total=d2.dd-d1.dd;
printf("Selisih hari adalah= %d",total);
}
else
{
printf("Data yang anda masukkan salah, seharusnya T1 dulu baru T2\n");
}
}
Tidak ada komentar:
Posting Komentar