Converting date and time to string in Open C++

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Platform(s): S60 3rd Edition, FP2
Keywords: time_t, tm, stringstream, use_facet, time_put, time_put::put(), time(), localtime(), asctime()
Created: aknyman (30 Sep 2008)
Last edited: hamishwillee (14 Jun 2012)



This code snippet shows how to convert a date and time to a string. The easiest way to get this string is to use the library function asctime() from the <ctime> header. The returned string has the following format: "Www Mmm dd hh:mm:ss yyyy". Another possibility is to use the time_put template class from the <locale> header and use a custom formatting for the date and time string.

Note: In order to use this code, you need to install the Open C/C++ plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib

Source file

#include <iostream>
#include <string>
#include <sstream>
#include <locale>
#include <ctime>
using namespace std;
string datetime_to_string(const tm& time, const char* format)
stringstream datetime;
// retrieve the time_put facet installed in the stream
const time_put<char>& writer =
use_facet< time_put<char> >(datetime.getloc());
int len = strlen(format);
//formats the contents of the tm time into the output stream datetime
if (writer.put(datetime, datetime, ' ',
&time, format, format + len).failed( ))
throw runtime_error("formatting date time failed!");
return datetime.str();
int main( )
string time_string;
time_t time_now = time(NULL);
struct tm * timeinfo;
timeinfo = localtime (&time_now);
//-- using the library function asctime()
cout << "Current local time and date: " << endl
<< asctime(timeinfo) << endl;
//-- using the own function datetime_to_string()
/* The list of % format specifiers:
a Abbreviated weekday name
A Full weekday name
b Abbreviated month name
B Full month name
c Date and time
d Day of the month
H Hour of the 24-hour day
I Hour of the 12-hour day
j Day of the year
m Month of the year
M Minutes after the hour
p AM/PM indicator
S Seconds after the minute
U Week of the year (Sunday)
w Day of the week
W Week of the year (Monday)
x Date MM/DD/YY
X Time HH/MM/SS (24-hour)
y Year of the century
Y Year
Z Time zone name

time_string = datetime_to_string(*timeinfo, "%A %B %d %H:%M:%S %Y");
cout << time_string << endl;
time_string = datetime_to_string(*timeinfo, "%Y/%m/%d %H:%M:%S");
cout << time_string << endl;
time_string = datetime_to_string(*timeinfo, "%x %X");
cout << time_string << endl;
catch (const exception& e)
cout << "Exception: " << e.what() << endl;
return 0;


The current date and time have been converted to four different formats and are shown on the screen.

This page was last modified on 14 June 2012, at 02:06.
78 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.