I need to get the current date and time in my emscripten code. I know there is emscripten_date_now(). But how would I get the day, the month, the year, and the time (hrs, minutes, and seconds) from that? (Or should I use the C/C++ time functions?).
Some operations that I need to perform is to be able to add a week. To change the month and year.
I am really stuck on this, so help will be greatly appreciated!
How do you get the current date and time in emscripten c++
Use std::chrono::system_clock::now() to get the current point in time.
how would I get the day, the month, the year, and the time (hrs, minutes, and seconds)
With the date library date library
#include <date/date.h>
#include <iostream>
int
main ()
{
using namespace date;
auto const currentTimePoint = std::chrono::system_clock::now ();
auto const currentDate = year_month_day{ floor<days> (currentTimePoint) };
std::cout << "year: " << currentDate.year () << std::endl;
std::cout << "month: " << currentDate.month () << std::endl;
std::cout << "day: " << currentDate.day () << std::endl;
auto const timeWithOutDate = make_time (currentTimePoint - date::floor<days> (currentTimePoint));
std::cout << "hour: " << timeWithOutDate.hours () << std::endl;
std::cout << "minute: " << timeWithOutDate.minutes () << std::endl;
std::cout << "second: " << timeWithOutDate.seconds () << std::endl;
}
Example output:
year: 2022
month: Jan
day: 24
hour: 11h
minute: 34min
second: 18s
Some operations that I need to perform is to be able to add a week
#include <date/date.h>
#include <iostream>
int
main ()
{
using namespace date;
auto now = floor<days> (std::chrono::system_clock::now ());
std::cout << "now in 1 week: " << now + weeks{ 1 } << std::endl;
}
Example output:
now in 1 week: 2022-01-31
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With