How to use difftime() to computes the number of seconds passed since the beginning of the month in C

1 Answer

0 votes
#include <stdio.h>
#include <time.h>
 
int main(void)
{
    time_t now;
    time(&now);
 
    struct tm begin;
    begin = *localtime(&now);
 
    begin.tm_hour = 0;
    begin.tm_min = 0;
    begin.tm_sec = 0;
    begin.tm_mday = 1;
    
    double seconds = difftime(now, mktime(&begin));
 
    printf("%.f seconds passed since the beginning of the month", seconds);

    return 0;
}
  
/*
run:
 
2305103 seconds passed since the beginning of the month

*/

 



answered Aug 27, 2016 by avibootz
edited Aug 27, 2016 by avibootz

Related questions

2 answers 129 views
129 views asked Feb 10, 2023 by avibootz
1 answer 84 views
84 views asked Jan 8, 2023 by avibootz
1 answer 183 views
...