How to read the system time and date in dos (with the time ticking).

Posted on 2004-11-06
Last Modified: 2008-01-16
Dear experts, professionals, and gurus

How to read the system time and date in dos?? Because i'm doing a library information system for my school assignment for MS-DOS and i want to have the time and date displayed (with the time ticking).

For your informaiton, i'm using bloodshed dev C++ compiler, so I hope that the solutions given to me is compilable with that compiler.

Question by:qweweng
    LVL 8

    Accepted Solution

    Windows has Windows-specific functions, but if you want to use the standard functions, something like this would work:

    #include <ctime>
    void PrintTime()
      time_t now = time(NULL); // Get the time
      struct tm *time_struct = localtime(&now); // Convert it to a struct that's easy to use
      char time_string[100] = {0}; // Create a variable to hold the time string
      strftime(time_string, 100, "%m/%d/%Y %H:%M:%S", time_struct); // Format the string
      cout << time_string << endl; // Output the string
    LVL 39

    Expert Comment


    /* MKTIME.C: The example takes a number of days
     * as input and returns the time, the current
     * date, and the specified number of days.

    #include <time.h>
    #include <stdio.h>

    void main( void )
       struct tm when;
       time_t now, result;
       int    days;

       time( &now );
       when = *localtime( &now );
       printf( "Current time is %s\n", asctime( &when ) );
       printf( "How many days to look ahead: " );
       scanf( "%d", &days );

       when.tm_mday = when.tm_mday + days;
       if( (result = mktime( &when )) != (time_t)-1 )
          printf( "In %d days the time will be %s\n",
                  days, asctime( &when ) );
          perror( "mktime failed" );


    Current time is Tue May 03 12:45:47 1994

    How many days to look ahead: 29
    In 29 days the time will be Wed Jun 01 12:45:47 1994

    You could also use strftime to convert a date/time to a string value.

    Regards, Alex
    LVL 19

    Expert Comment

    I suppose you could do something like this:

    #include <ctime>
    #include <iostream>
    #include <windows.h>
    #include <process.h>

    using namespace std;

    unsigned __stdcall ThreadFunc(void* pParam)
          HANDLE hConsole = GetStdHandle (STD_OUTPUT_HANDLE); // Get a handle to standard output
          COORD pos = {0, 0};      
          while (true) {
                SetConsoleCursorPosition (hConsole, pos);
                time_t curr = time(0);
                cout << ctime(&curr) << endl;

    int main()
          HANDLE hThread;

          hThread = (HANDLE)_beginthreadex(NULL, NULL, &ThreadFunc, NULL, 0, 0);

          while (true) {


    You need to be running a secondary thread in which you update the time.  Here I have set the thread to update the time once per second, and have the time output on the top line of the console (0,0).  


    Expert Comment

         char szDate[128];
         SYSTEMTIME st;
         GetLocalTime(& st);
         sprintf(szDate, "%02d:%02d:%02d %4d/%02d/%02d", st.wHour, st.wMinute, st.wSecond,  st.wYear, st.wMonth, st.wDay);


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now