Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C++

Idiom #99 Format date YYYY-MM-DD

Assign to the string x the value of the fields (year, month, day) of the date d, in format YYYY-MM-DD.

#include <string>
#include <ctime>
#include <iostream>
#include <iomanip>
int main()
{
	char x[32]{};
	time_t a = time(nullptr);
	struct tm d;
	if (localtime_s(&d, &a) == 0) {
		strftime(x, sizeof(x), "%F", &d);
		std::cout << x << std::endl;
	}

	return 0;

}

//Microsoft's localtime_s, returns zero on success, an error code on failure.
with Ada.Calendar.Formatting;
X : constant String :=
    Ada.Calendar.Formatting.Image (D) (1 .. 10);

The Image function returns time as well therefore the slice.

New implementation...