#include <assert.h>
#include <stdio.h>
#include <time.h>
int main(void) {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char s[64];
assert(strftime(s, sizeof(s), "%x", tm));
printf("%s\n", s);
return 0;
}
Output:
vadakkodan@vadakkodanz:~/Desktop/Bahuleyans_Supermarket$ ./t
02/13/22
Some format specifiers for strftime() are shown as follows :
%x = Preferred date representation
%I = Hour as a decimal number (12-hour clock).
%M = Minutes in decimal ranging from 00 to 59.
%p = Either “AM” or “PM” according to the given time value, etc.
%a = Abbreviated weekday name
%^a = Abbreviated weekday name in capital letters
%A = Full weekday name
%b = Abbreviated month name
%^b = Abbreviated month name in capital letters
%B = Full month name March
%c = Date and time representation
%d = Day of the month (01-31)
%H = Hour in 24h format (00-23)
%I = Hour in 12h format (01-12)
%j = Day of the year (001-366)
%m = Month as a decimal number (01-12)
%M = Minute (00-59)
man strftime
Will give more information about it.
A modified code for my project is added below.
#include <assert.h>
#include <stdio.h>
#include <time.h>
int main(void) {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char s[64];
char tim[64];
assert(strftime(s, sizeof(s), "Date: %d-%b-%G", tm));
assert(strftime(tim, sizeof(tim),"Time: %I:%M %p", tm));
printf("%s\n", s);
printf("%s\n", tim);
return 0;
}
Output:
vadakkodan@vadakkodanz:~/Desktop/Bahuleyans_Supermarket$ ./t
Date: 13-Feb-2022
Time: 04:04 PM