Jump to content
xisto Community
Sign in to follow this  
livepcportal

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

Recommended Posts

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

#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

#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. ;) 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.

Share this post


Link to post
Share on other sites

You will have to do it inside a loop. Putting your code inside a loop could look like this:

#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   }}

Share this post


Link to post
Share on other sites

Thank you nooc9 for giving such an easy solution! :lol: 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!

Share this post


Link to post
Share on other sites

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.

Edited by nooc9 (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.