Tutorials Home   >   Practical Programming Skills   >   Working with Dates and Time

Working with Dates and Time

1. Introduction

Working with dates and time in C allows programs to handle real-world time-related information such as current date, current time, delays, timestamps, and time differences. Date and time operations are useful in applications like:

  • Attendance systems

  • Banking and transaction logs

  • Scheduling applications

  • File timestamp management

C provides built-in functions through the time.h header file to work with dates and time.


2. The time.h Header File

To work with dates and time, C uses the time.h header file.

Syntax

#include <time.h>

This header file provides:

  • Data types

  • Structures

  • Functions for time operations


3. The time_t Data Type

Definition

time_t is a data type used to store calendar time, which represents the number of seconds passed since
January 1, 1970 (Epoch time).

Example

time_t currentTime;
currentTime = time(NULL);
  • time(NULL) returns the current time


4. Getting Current Date and Time

Example Program

#include <stdio.h>
#include <time.h>
int main() {
time_t t;
time(&t);printf(“Current date and time: %s”, ctime(&t));
return 0;
}

Explanation

  • time(&t) gets current time

  • ctime() converts time into readable format


5. The struct tm

C uses a structure called struct tm to store date and time components.

Members of struct tm

Member Description
tm_sec Seconds (0–59)
tm_min Minutes (0–59)
tm_hour Hours (0–23)
tm_mday Day of month (1–31)
tm_mon Month (0–11)
tm_year Years since 1900
tm_wday Day of week (0–6)
tm_yday Day of year (0–365)

6. Using localtime() Function

localtime() converts time_t into local date and time.

Example

#include <stdio.h>
#include <time.h>
int main() {
time_t t;
struct tm *info;time(&t);
info = localtime(&t);

printf(“Date: %d-%d-%d\n”,
info->tm_mday,
info->tm_mon + 1,
info->tm_year + 1900);

printf(“Time: %d:%d:%d\n”,
info->tm_hour,
info->tm_min,
info->tm_sec);

return 0;
}


7. Formatting Date and Time Using strftime()

strftime() is used to format date and time according to user-defined format.

Syntax

strftime(string, size, format, timeptr);

Common Format Specifiers

Specifier Meaning
%d Day
%m Month
%Y Year
%H Hour
%M Minute
%S Second

Example

char buffer[50];
strftime(buffer, 50, "%d-%m-%Y %H:%M:%S", info);
printf("%s", buffer);

8. Using gmtime() Function

gmtime() converts time to UTC (Greenwich Mean Time).

Example

struct tm *utc;
utc = gmtime(&t);

9. Calculating Time Difference Using difftime()

difftime() calculates the difference between two times.

Syntax

double difftime(time2, time1);

Example

time_t start, end;
time(&start);
/* code delay */time(&end);
printf(“Time taken: %.2f seconds”, difftime(end, start));


10. Creating Custom Date and Time

Example

struct tm customTime = {0};
customTime.tm_year = 2024 - 1900;
customTime.tm_mon = 5;
customTime.tm_mday = 10;
time_t t = mktime(&customTime);

11. Common Date and Time Functions

Function Purpose
time() Get current time
ctime() Convert time to string
localtime() Local date and time
gmtime() UTC time
strftime() Format date/time
difftime() Time difference
mktime() Convert struct tm to time_t

12. Real-Life Applications

  • Attendance and payroll systems

  • Digital clocks and timers

  • Logging systems

  • File management systems


13. Common Errors While Working with Date and Time

  • Forgetting to include time.h

  • Not adding 1900 to tm_year

  • Not adding 1 to tm_mon

  • Using wrong format specifiers


14. Advantages of Using Date and Time Functions

  • Accurate time management

  • Real-world application support

  • Easy formatting

  • Time difference calculation


15. Best Practices

  • Always use time.h

  • Format date and time properly

  • Use difftime() for calculations

  • Avoid hardcoding dates


16. Conclusion

Working with dates and time in C allows programs to handle real-world time-based data efficiently. By using time.h, time_t, struct tm, and related functions, programmers can display, format, and calculate dates and time accurately. This concept is essential for building professional and practical C applications.