How to Use Date and Time in a C++ Program

Dates and times have frequent usage in C++ programs. Windows programs use several different time formats: System time, local time, file time, Windows time and MS-DOS time. The Run Time Library of C++ offers various tools to extract and manipulate time formats easily. They are defined in the time.h header file. This tutorial demonstrates the usage of some formats and tools.

Things You'll Need

  • Intermediate level of C++
  • C++ compiler with IDE, such as Visual Studio 2008
Show More

Instructions

    • 1

      Extract the current date and time using _strdate and _strtime. This is the simplest and one of the most frequently used date-time operations in C++:

      #include < iostream.h >

      #include < time.h >

      void main() {

      char sdate[9];

      char stime[9];

      _strdate( sdate );

      _strtime( stime );

      cout << "time: " << stime << " date: " << sdate << endl;

      }

    • 2

      Understand System time by looking at the fields of the _SYSTEMTIME struct. Note the use of the typedef keyword to define the struct as type SYSTEMTIME:

      typedef struct _SYSTEMTIME {

      WORD wYear;

      WORD wMonth;

      WORD wDayOfWeek;

      WORD wDay;

      WORD wHour;

      WORD wMinute;

      WORD wSecond;

      WORD wMilliseconds;

      } SYSTEMTIME;

    • 3

      Display universal time and date using the SYSTEMTIME type and the GetSystemTime function:

      #include < iostream.h >

      #include < Windows.h >

      using namespace std;

      int main(){

      SYSTEMTIME* p_st = new SYSTEMTIME;

      GetSystemTime(p_st);

      cout << "Year: " << p_st->wYear << endl;

      cout << "Month: " << p_st->wMonth << endl;

      cout << "Day: " << p_st->wDate << endl;

      cout << "Hour: " << p_st->wHour << endl;

      cout << "Minutes: " << p_st->wMinute << endl;

      cout << " Seconds: " << p_st->wSeconds << endl;

      cout << "Milliseconds: " << p_st->wMilliseconds << endl;

      }

    • 4

      Use the function FileTimeToSystemTime to express time as the number of nanoseconds that have elapsed since January 1, 1601. The function writes the result to a FILETIME type and converts it to a human-readable SYSTEMTIME type. Note that this function accepts both types as pointers:

      BOOL WINAPI FileTimeToSystemTime(

      __in const FILETIME* pFT,

      __out SYSTEMTIME* pST

      );

Related Searches:

Resources

Comments

You May Also Like

  • How to Get a System Date in C#

    It's critical not to know the current system date in a Microsoft Visual C# program, especially if it's being used to save...

  • Fun Date Ideas in Vancouver, B.C.

    Fun Date Ideas in Vancouver, B.C.. Date night in Vancouver, the largest city in the province of British Columbia, can be an...

  • How to Change Time and Date on Windows XP

    Windows XP has lots of features that people tend to take for granted. The time and date features are on the screen,...

  • How To Create a Date in C#

    Microsoft's C#, pronounced C-Sharp, programming language provides a middle ground for developers between the complexity and power of C++ and the simplicity...

  • How to Insert a Dynamic Date and Time in Excel

    A dynamic date and time can be quickly inserted in a Microsoft Excel 2007 spreadsheet. A formula is used to instruct Excel...

  • Real Time Court Reporting Schools

    As with virtually every other job, court reporting has advanced with computer technology. With real time court reporting, a stenographer's notes are...

  • How to Add Minutes to DateTime

    C++ is a programming language featuring a vast library of functions that work with Microsoft Windows, and has many built-in value types....

  • How to Fix Date and Time

    If your PC has the wrong date and time setting, then all the files you create and save will have the wrong...

Related Ads

Featured