You are here:

C++/Date class


I am trying to locate examples of writing a program that gets the date and will store the date into a variable. Upon running the program, the date should display in this format:

Jan 01, 1980

I am not sure how to write this program to make it run. I am using MSVS C++. I would like to know if you have any ideas or examples that might be of some help to me. Thank you.


Christy , Thank you for your question.

There are many ways to handle dates.

You can use functions such as "time" and "mktime" (using datatypes "time_t" and "tm") to manipulate dates. You can print a date stored in a "tm" struct using any formatting you wish.

Here is some source code to show one way to solve your problem. IMPORTANT NOTE: This Windows code may not compile because it is dependent on other parts of my personal application library. It should be sufficient to give you some ideas; I cannot supply you with all of the source code needed to make this example actually work. Note the failure of to provide for indenting source code.

//   ----------------------------------------------------------------------------
//   Name:         function GetShortDateString
//   Description:   Get date string only (like "3/29/00").
//   ----------------------------------------------------------------------------
PCSTR          CTime::GetShortDateString(BOOL UseLeadingZeros /*= FALSE*/)
  char *         DateStr = GetNewTempPtr(64);
  long         Year;
  PCSTR         Format;

  if (!PrepareForGet())
     return UNDEFSTR;

  if (UseLeadingZeros)
     Format = "%.2ld/%.2ld/%.2ld";
     Format = "%ld/%ld/%.2ld";
   Year = _Tm.tm_year % 100;
  wsprintf(DateStr, Format, (long)(_Tm.tm_mon + 1),
     long(_Tm.tm_mday), (long)Year);
  return DateStr;
  } // CTime::GetShortDateString

David Spector


All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 All rights reserved.