c语言什么是函数 c语言 用库函数计算两日期相差的天数
c语言 用库函数计算两日期相差的天数
c语言 用库函数计算两日期相差的天数
#include <stdio.h>
struct date
{
int year;
int month;
int day;
};
int main(void)
{
int isPrime(int year);
int dateDiff(struct date mindate,struct date maxdate);
struct date mindate,maxdate;
int days;
printf("please input the one date:");
scanf("%i-%i-%i",&mindate.year,&mindate.month,&mindate.day);
printf("please input other day:");
scanf("%i-%i-%i",&maxdate.year,&maxdate.month,&maxdate.day);

days=dateDiff(mindate,maxdate);
printf("the day is:%dn",days);
return 0;
}
/************************************************************************/
/************************************************************************/
int isPrime(int year)
{
if ((year%4==0&&year%100!=0)||(year%400==0))
{
return 1;
}
else
{
return 0;
}
}
int dateDiff(struct date mindate,struct date maxdate)
{
int days=0, flag=1;
const int primeMonth[12]={31,29,31,30,31,30,31,31,30,31,30,31};
const int notPrimeMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
/************************************************************************/
/************************************************************************/
struct date tmp;
if ((mindate.year>maxdate.year)|| (mindate.year==maxdate.year&&mindate.month>maxdate.month)||(mindate.year==maxdate.year&&mindate.month==maxdate.month&&mindate.day>maxdate.day))
{
tmp=mindate;
mindate=maxdate;
maxdate=tmp;
}
int maxmonth,minmonth;
/************************************************************************/
/*一前一后刚好N年,算出2005-2-22到2005-7-8的天数,然后用总年*36(5|6)减掉) */
/************************************************************************/
if (maxdate.month<mindate.month)
{
maxmonth=mindate.month;
minmonth=maxdate.month;
flag=-1;
}
else
{
maxmonth=maxdate.month;
minmonth=mindate.month;
flag=1;
}
/************************************************************************/
/************************************************************************/
for(int j=mindate.year;j<maxdate.year;++j)
{
if (isPrime(j)==1)
{
days+=366;
}
else
days+=365;
}
/************************************************************************/
/************************************************************************/
int day;
if(isPrime(maxdate.year)==1)
{
for(int i=minmonth;i<maxmonth;i++)
{
day=primeMonth[i-1]*flag;
days=days+day;
}
days=days+maxdate.day-mindate.day;
}
else
{
for (int i=minmonth;i<maxmonth;i++)
{
day=notPrimeMonth[i-1]*flag;
days=days+day;
}
days=days+maxdate.day-mindate.day;
}
return days;
}
C语言求两个日期相差的天数
计算两个年月日之间的天数,思路是分别算出日期的总天数然后相减。
要考虑闰年的情况,判断闰年的口诀:4年一闰,100年不闰,400年再闰。
((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
网上找了一个(偷懒= =!),修改下如下:
#include <stdio.h>
int sum(int y,int m,int d)
{
unsigned char x[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,s=0;
for(i=1;i<y;i++)
if(i%4==0 && i%100!=0 || i%400==0)
s+=366;闰年
else
s+=365;平年
if(y%4==0 && y%100!=0 || y%400==0)
x[2]=29;
for(i=1;i<m;i++)
s+=x[i];整月的天数
s+=d;日的天数
return s;返回总天数,相对公元1年
}
void main()
{
unsigned char y1,m1,d1,y2,m2,d2;
int s1,s2;
printf("输入第一个年 月 日:");
scanf("%d %d %d",&y1,&m1,&d1);
printf("输入第二个年 月 日:");
scanf("%d %d %d",&y2,&m2,&d2);
s1=sum(y1,m1,d1);
s2=sum(y2,m2,d2);
if (s1 > s2)
printf("相差天数:%ldn",s1-s2);
else
printf("相差天数:%ldn",s2-s1);
}
以上代码VC6编译测试通过。
虽然这个思路显得有些笨,但是其它算法,代码太长太复杂,要考虑多种情况,不如直接算两个日期距离公元元年1月1日的天数,然后相减
JAVA计算两个日期之间相差的天数?
字符串转换成 java.util.Calendar ,而后使用 Calendar的方法
PHP中怎样计算两个日期相差的天数
<?php/** * 求两个日期之间相差的天数 * (针对1970年1月1日之后,求之前可以采用泰勒公式) * @param string $day1 * @param string $day2 * @return number */function diffBeeenTwoDays ($day1, $day2){ $second1 = strtotime($day1); $second2 = strtotime($day2); if ($second1 < $second2) { $tmp = $second2; $second2 = $second1; $second1 = $tmp; } return ($second1 - $second2) / 86400;}$day1 = "2013-07-27";$day2 = "2013-08-04";$diff = diffBeeenTwoDays($day1, $day2);echo $diff."n";java如何计算两个日期之间相差的天数
看完了jdk的help,发现sun没有提供这样的一个函数,朋友给了几个实现方法取得剩余天数
SimpleDateFormat df=new SimpleDateFormat("yyyymmdd");
Date d0=new java.util.Date();
Date d1=df.parse(end_date);
long time0=d0.getTime();
long time1=d1.getTime();
System.out.println((time1-time0)/(1000*60*60*24)); 这样算两个时间相差的天数比较好 /**
* 计算两个日期之间相差的天数** @param date1
* @param date2* @return*/public static int diffdates(Date date1, Date date2) {
int result = 0;
ElapsedTime et = new ElapsedTime(); GregorianCalendar gc1 = new GregorianCalendar();
GregorianCalendar gc2 = new GregorianCalendar(); gc1.setTime(date1);
gc2.setTime(date2);
result = et.getDays(gc1, gc2); return result;}然后ElapseTime中的方法是:
public int getDays(GregorianCalendar g1, GregorianCalendar g2) {
int elapsed = 0;
GregorianCalendar gc1, gc2; if (g2.after(g1)) {
gc2 = (GregorianCalendar) g2.clone();
gc1 = (GregorianCalendar) g1.clone();} else {gc2 = (GregorianCalendar) g1.clone();
gc1 = (GregorianCalendar) g2.clone();
} gc1.clear(Calendar.MILLISECOND);
gc1.clear(Calendar.SECOND);
gc1.clear(Calendar.MINUTE);
gc1.clear(Calendar.HOUR_OF_DAY); gc2.clear(Calendar.MILLISECOND);
gc2.clear(Calendar.SECOND);
gc2.clear(Calendar.MINUTE);
gc2.clear(Calendar.HOUR_OF_DAY); while (gc1.before(gc2)) {
gc1.add(Calendar.DATE, 1);elapsed++;}return elapsed;}其实使用joda最简单public boolean isRentalOverdue(DateTime datetimeRented) {
c语言怎么求两个日期相差的秒数,日期格式20140325150630和20140324150000
#include <stdio.h>
#include <time.h>
int main(void)
{
struct tm time_cha;
time_t t1,t2;
long int d;
int year, month, day,hour,min,sec;
scanf("%4d%2d%2d%2d%2d%2d",&year,&month,&day,&hour,&min,&sec);
time_cha.tm_year = year - 1900; tm结构记录年为实际-1900
time_cha.tm_mon = month - 1;
time_cha.tm_mday = day;
time_cha.tm_hour = hour;
time_cha.tm_min = min;
time_cha.tm_sec = sec;
t1 = mktime(&time_cha);获得从1970年1月1日0时0分0秒以来过去的时间,秒
scanf("%4d%2d%2d%2d%2d%2d",&year,&month,&day,&hour,&min,&sec);
time_cha.tm_year = year - 1900; tm结构记录年为实际-1900
time_cha.tm_mon = month - 1;
time_cha.tm_mday = day;
time_cha.tm_hour = hour;
time_cha.tm_min = min;
time_cha.tm_sec = sec;
t2 = mktime(&time_cha);
d=t1-t2;经过的时间差(秒)
printf("%ldn", d);
return 0;
}
ASP计算两个日期之间相差天数的函数?
Datediff("d",date1,date2)
'date1和date2是你要计算的两个时间,该函数直接返回相差天数.
C语言用什么函数可以得到输入的日期与1970年相差的秒
printf 格式输出到屏幕fprintf 格式输出到磁盘scanf 从屏幕格式输入fscanf 从磁盘格式输入putchar 字符输出到屏幕puts 字符串输出到屏幕fputc 字符输出到磁盘fputs 字符串输出到磁盘getchar 从屏幕得到一个字符gets 从屏幕得到一个字符串fgetc 从磁盘得到一个字符fgets 从磁盘得到一个字符串
退出fun函数fun函数内的局部变量a生命期结束c语言,输入两个日期,输出两个日期之间相