簡體   English   中英

兩個日期之間的天數有所不同?

[英]Getting difference between two dates in number of days?

比較兩天時,我得到的日子不正確。 在這里,我得到699天而不是730天,而dd=14,mm=8,yy=2014

time_t t1, t2;
struct tm my_target_date;

 my_target_date.tm_sec = 0;
 my_target_date.tm_min = 0;
 my_target_date.tm_hour = 0;
 my_target_date.tm_mday = 14;
 my_target_date.tm_mon = 8;
 my_target_date.tm_year = 112; /* Date today */
 t1 = mktime (&my_target_date);
 t2 = time (NULL);
 sprintf (sbuff2,"Number of days since target date : %ld\n", (t2 - t1) / 86400);

struct tm

int tm_mday; // day of the month — [1, 31]
int tm_mon; // months since January — [0, 11]

tm_mday1開始,但tm_mon0開始。 因此, my_target_date.tm_mon = 8; 實際上是九月,您要休假一個月。

struct tm結構是-

struct tm {
           int tm_sec;         /* seconds */
           int tm_min;         /* minutes */
           int tm_hour;        /* hours */
           int tm_mday;        /* day of the month */
           int tm_mon;         /* month */
           int tm_year;        /* year */
           int tm_wday;        /* day of the week */
           int tm_yday;        /* day in the year */
           int tm_isdst;       /* daylight saving time */
       };

tm結構的成員是:

   tm_sec    The  number of seconds after the minute, normally in the range 0 to 59, but can be up
             to 60 to allow for leap seconds.

   tm_min    The number of minutes after the hour, in the range 0 to 59.

   tm_hour   The number of hours past midnight, in the range 0 to 23.

   tm_mday   The day of the month, in the range 1 to 31.

   tm_mon    The number of months since January, in the range 0 to 11.

   tm_year   The number of years since 1900.

   tm_wday   The number of days since Sunday, in the range 0 to 6.

   tm_yday   The number of days since January 1, in the range 0 to 365.

   tm_isdst  A flag that indicates  whether  daylight  saving  time  is  in  effect  at  the  time
             described.  The value is positive if daylight saving time is in effect, zero if it is
             not, and negative if the information is not available.

如果將輸入設為dd=14,mm=8,yy=2014 ,則表示輸入dd=14,mm=8,yy=2014 9月14日,因為在tm_mon month中從0開始。 因此它將計算從September 14th 2014的天數。 因此您在這31天之間不見了。

輸入為dd=14,mm=7,yy=2014

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM