Date formatting? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Date formatting?

https://code.sololearn.com/c454ZOctiB5J/?ref=app How can I format the date like "02/09/1998"? Getting an error when I try it.

2nd Mar 2019, 10:14 PM
Daniel Cooper
Daniel Cooper - avatar
4 Answers
+ 3
#include <iomanip> std::cout << std::setw( 2 ) << std::setfill( '0' ) << 2;
2nd Mar 2019, 10:35 PM
Dennis
Dennis - avatar
+ 1
//Try this one it works cout <<'0'<<month << '/' << '0'<<day << '/' << year << "\n\n";
2nd Mar 2019, 10:34 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 1
Wouldn't both of those add a zero even if the number is over 9?
2nd Mar 2019, 10:47 PM
Daniel Cooper
Daniel Cooper - avatar
+ 1
My solution wouldn't. setw kinda creates an 'array', for ease of explanation, of width 2 in this case. [ . . ] setfill fills em with '0' in this case [ '0' '0' ] Whatever you put into the stream after that overwrites whatever is in that array from the right << 2 [ '0' '2' ] If I did << 21 the array would become [ '2' '1' ] No zeros there.
2nd Mar 2019, 10:51 PM
Dennis
Dennis - avatar