🕐Beginner

Display Dates in Different Formats

Date formatting

Display dates in various formats using strftime.

Program Code

calendar_formats.c
C
1#include <stdio.h>
2#include <time.h>
3
4int main() {
5 time_t now;
6 struct tm *local;
7 char buffer[100];
8
9 time(&now);
10 local = localtime(&now);
11
12 printf("Date Formats:\n");
13 printf("=============\n\n");
14
15 strftime(buffer, sizeof(buffer), "%d/%m/%Y", local);
16 printf("DD/MM/YYYY: %s\n", buffer);
17
18 strftime(buffer, sizeof(buffer), "%m/%d/%Y", local);
19 printf("MM/DD/YYYY: %s\n", buffer);
20
21 strftime(buffer, sizeof(buffer), "%Y-%m-%d", local);
22 printf("YYYY-MM-DD: %s\n", buffer);
23
24 strftime(buffer, sizeof(buffer), "%B %d, %Y", local);
25 printf("Month DD, YYYY: %s\n", buffer);
26
27 strftime(buffer, sizeof(buffer), "%A, %B %d, %Y", local);
28 printf("Full format: %s\n", buffer);
29
30 strftime(buffer, sizeof(buffer), "%b %d '%y", local);
31 printf("Short format: %s\n", buffer);
32
33 return 0;
34}
Output

Date Formats:

=============

 

DD/MM/YYYY: 16/12/2024

MM/DD/YYYY: 12/16/2024

YYYY-MM-DD: 2024-12-16

Month DD, YYYY: December 16, 2024

Full format: Monday, December 16, 2024

Short format: Dec 16 '24