flutter date format
#include <iostream>
#include <iomanip>
#include <sstream>
#include <ctime>
int main() {
// Get the current time
std::time_t now = std::time(0);
// Convert the current time to struct tm for formatting
std::tm* timeinfo = std::localtime(&now);
// Create a string stream to format the date
std::stringstream ss;
// Set the desired date format using std::put_time
ss << std::put_time(timeinfo, "%Y-%m-%d %H:%M:%S");
// Convert the stringstream to a string containing the formatted date
std::string formattedDate = ss.str();
// Output the formatted date
std::cout << "Formatted date: " << formattedDate << std::endl;
return 0;
}
Explanation:
- #include <iostream>
: Includes the input/output stream for handling input and output operations.
- #include <iomanip>
: Includes input/output manipulators for controlling the format of input and output.
- #include <sstream>
: Includes the stringstream class for string-based input/output.
- #include <ctime>
: Includes functions and types to work with date and time information.
- std::time_t now = std::time(0);
: Gets the current time using std::time
.
- std::tm* timeinfo = std::localtime(&now);
: Converts the current time to a tm
structure to handle date and time components.
- std::stringstream ss;
: Creates a stringstream object to handle string-based input/output.
- ss << std::put_time(timeinfo, "%Y-%m-%d %H:%M:%S");
: Uses std::put_time
to format the date and time according to the specified format ("%Y-%m-%d %H:%M:%S"
) and stores it in the stringstream.
- std::string formattedDate = ss.str();
: Converts the stringstream content to a string containing the formatted date.
- std::cout << "Formatted date: " << formattedDate << std::endl;
: Outputs the formatted date to the console.