Matt
Matt

Reputation: 482

Format a string as a date

I have a string date and I need to format it as a date (dd.mm.yyyy).How can I do that?Are there some functions that can simplify formatting?

LE:"Write a c++ program which helps you to manage tasks...A task has a unique format,id,description,date(string having the format "dd.MM.yyyy", eg 10.07.2013)."

Upvotes: 0

Views: 677

Answers (3)

psykoblast
psykoblast

Reputation: 116

try this , this give you the system time in a char[32]

time_t curtime;
struct tm *loctime;
char date_str[32];

curtime = time (NULL);

/* Convert it to local time representation. */
loctime = localtime (&curtime);
strftime (date_str, 32, "%d.%m.%Y", loctime);

Upvotes: 1

EmptyData
EmptyData

Reputation: 2576

C++ standard library does not provide time data type but you can do by including ctime in header file.

#include <ctime>
#include <iostream>
using namespace std;

int main() {
    time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );
    cout << (now->tm_year + 1900) << '-' 
         << (now->tm_mon + 1) << '-'
         <<  now->tm_mday
         << endl;
}

Upvotes: 0

seshuk
seshuk

Reputation: 182

You have to be more specific of the C++ that you are using. If its C++/CLI, then you can use

DateTime::Parse

If its not C++/CLI and you know the exact format of the string you can use

sscanf(....)

and extract individual items to assign to time struct.

Upvotes: 0

Related Questions