Is there a cross-platform way to get the current date and time in C++?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- How do you make an installer for your python progr
- Selecting only the first few characters in a strin
- Libgdx - Check if a key is being held down?
- What exactly do pointers store? (C++)
Yes and you can do so with formatting rules specified by the currently-imbued locale:
You can also directly use
ctime()
:You can use the following code to get the current system date and time in C++ :
PS: Visit this site for more information.
Get the current time either using
std::time()
orstd::chrono::system_clock::now()
(or another clock type).std::put_time()
(C++11) andstrftime()
(C) offer a lot of formatters to output those times.The sequence of the formatters matters:
The formatters of
strftime()
are similar:Often, the capital formatter means "full version" and lowercase means abbreviation (e.g. Y: 2017, y: 17).
Locale settings alter the output:
Possible output (Coliru, Compiler Explorer):
I've used
std::gmtime()
for conversion to UTC.std::localtime()
is provided to convert to local time.Heed that
asctime()
/ctime()
which were mentioned in other answers are marked as deprecated now andstrftime()
should be preferred.