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

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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

/* 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
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).  

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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.