Dave's Brain

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

Date: 2018may14
Language: C/C++

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.
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

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.