Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I printf a date in C?

Tags:

c

date

ctime

I'm trying to print a date from a string like "01/01/01" and get something like "Monday First January 2001.

I found something with the man of ctime but really don't get it how to use it.

Any help ?

Thanks,

like image 401
Difender Avatar asked Jun 11 '14 11:06

Difender


People also ask

How will you print the date using C?

One way to check this: Just initialize the variable and then take input. int date = 0, month = 0, year = 0; scanf("%d/%d/%d", &date, &month, &year); Now, if you give 10-12-2016 as input, you will get 10-0-0 as output.

What is %U in C printf?

Explanation: In the above program, variable c is assigned the character 'a'. In the printf statement when %u is used to print the value of the char c, then the ASCII value of 'a' is printed. Case 2: Print float value using %u.

What is %f in printf in C?

The f in printf stands for formatted, its used for printing with formatted output.

What is %s %d %F in C?

%s refers to a string %d refers to an integer %c refers to a character. Therefore: %s%d%s%c\n prints the string "The first character in sting ", %d prints i, %s prints " is ", and %c prints str[0].


1 Answers

You can use strptime to convert your string date to struct tm

struct tm tm;
strptime("01/26/12", "%m/%d/%y", &tm);

And then print struct tm in the appropriate date format with strftime

char str_date[256];
strftime(str_date, sizeof(str_date), "%A, %d %B %Y", &tm);
printf("%s\n", str_date);
like image 154
HAL Avatar answered Sep 17 '22 04:09

HAL