Nov 22, 2009

Can You Print And Update Current Date And Time - Need help in updating time and date

free web hosting
Open Discussion > MODERATED AREA > Computers > Programming Languages > C/C++ Programming

Can You Print And Update Current Date And Time - Need help in updating time and date

livepcportal
If you don't know how to print current date and time on the screen then this post might be helpful for you. Let us learn printing current date and time on screen. I have divided this into two parts, one will show you how to print current date and other one will show you how to print current time.

Printing Current Date on Screen
CODE
#include<conio.h>
#include<iostream.h>
#include<dos.h>
void main()
{
clrscr();
struct date d;
getdate(&d);
int d=d.da_day;
int m=d.da_mon;
int y=d.da_year;
cout<<d<<"/"<<m<<"/"<<y;
getch();
}


Printing Current Time on Screen
CODE
#include<conio.h>
#include<iostream.h>
#include<dos.h>
void main()
{
clrscr();
struct dostime_t t;
_dos_gettime(&t);
int h=t.hour;
int min=t.minute;
int s=t.second;
cout<<h<<":"<<min<<":"<<s;
getch();
}

I hope now you will be able to print current date and time on screen. But can you make the date and time continuously updating itself in the same program? If yes, then tell me because I don't know how to do it. sad.gif Also, if you know any other method of displaying date and time don't forget to tell me. I want to learn more.

Notice from truefusion:
All code needs to be placed within code bbcode.

 

 

 


Comment/Reply (w/o sign-up)

nooc9
You will have to do it inside a loop. Putting your code inside a loop could look like this:
CODE
#include<conio.h>
#include<iostream.h>
#include<dos.h>
#include<time.h>

void main()
{
   clrscr();
   struct date d;
   truct dostime_t t;
  
   for(;;) // infinite loop
   {
      getdate(&d);
      _dos_gettime(&t);

      int d=d.da_day;
      int m=d.da_mon;
      int y=d.da_year;
      int h=t.hour;
      int min=t.minute;
      int s=t.second;

      // append carriage return to output so we can continue printing
      // at the beginning of the same line
      cout<<d<<"/"<<m<<"/"<<y<<" "<<h<<":"<<min<<":"<<s<<"      \r";
      sleep(1); // sleep for 1 second
   }
}

 

 

 


Comment/Reply (w/o sign-up)

livepcportal
Thank you nooc9 for giving such an easy solution! rolleyes.gif
I have also thought about this earlier but never tried it practically.
However there is a disadvantage of this as the statements written outside the for loop will never get executed as compiler will never come out of the for loop so I can't use this in any other program where I need to perform some more options. For example I am making a program which will perform arithmetic operations on one side and at the same time displaying and updating current date and time. Thats not gonna work by this way for sure!
So, if there is any other way of doing this I would be thankful!

Comment/Reply (w/o sign-up)

nooc9
I don't know much about dynamic terminal interfaces, but I guess what you're looking for is something like curses. There is a portable curses clone called DPCurses you should check out.
One alternative is to write a GUI application. Ther is also the alternativ eto clear the screen every frame, but thats kind of not cool.

Comment/Reply (w/o sign-up)



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


Searching Video's for Can, You, Print, And, Update, Current, Date, And, Time
See Also,
advertisement


Can You Print And Update Current Date And Time - Need help in updating time and date

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com