master
master

Reputation: 1

How to display date using C++?

How can I display the date using the function "MessageBox"?

Upvotes: 0

Views: 1003

Answers (3)

detunized
detunized

Reputation: 15289

For example like this (I assumed you asked about native Windows API):

// Get current time
SYSTEMTIME now;
GetLocalTime(&now);

// Format the date using the default user language
TCHAR buffer[1024];
GetDateFormat(
    MAKELCID(LANG_USER_DEFAULT, SORT_DEFAULT), 
    0, 
    &now, 
    NULL, 
    buffer, 
    1024
);

// Show it in a message box
MessageBox(HWND_DESKTOP, buffer, _T("Today"), MB_OK);

It's also possible to ask GetDateFormat to calculate the buffer length required to store the output. To do that pass NULL and 0 as last two parameters:

int length = GetDateFormat(
    MAKELCID(LANG_USER_DEFAULT, SORT_DEFAULT), 
    0, 
    &now, 
    NULL, 
    NULL, 
    0
);

Upvotes: 0

SwDevMan81
SwDevMan81

Reputation: 49978

DateTime dateTime = DateTime::Now;
MessageBox::Show(dateTime.ToString());

Other ToXString() functions can be found here

Upvotes: 0

Nick Banks
Nick Banks

Reputation: 4408

Here is a link for several different ways to get the date and time: Date & Time

Copied from site above:

Definition (from windows):

typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;

Implementation:

SYSTEMTIME st;
GetSystemTime(&st);
// You format how you want

Upvotes: 1

Related Questions