Browse - Programming Tips - C/C++: How do I display the 4-digit year?

Date: 2018may14
Language: C/C++
Level: beginner

Q.  C/C++: How do I display the 4-digit year?

A.  Use

#include <time.h> char buf[100]; struct tm here; const time_t now = time(NULL); localtime_r(&now, &here); strftime(buf, size(buf), "%Y", &here); // You almost certainly don't want %G.
Or
snprintf(buf, sizeof(buf), "%d", here->tm_year + 1900); // Don't forget to add 1900
Hacker News discusssion. https://news.ycombinator.com/item?id=17066739