Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Can You Print And Update Current Date And Time


3 replies to this topic

#1 livepcportal

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 221 posts
  • Gender:Male
  • Location:India
  • myCENT:40.55

Posted 24 August 2009 - 03:44 PM

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.


#2 nooc9

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 31 posts

Posted 06 September 2009 - 07:42 PM

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


#3 livepcportal

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 221 posts
  • Gender:Male
  • Location:India
  • myCENT:40.55

Posted 20 September 2009 - 11:24 AM

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!

#4 nooc9

    Newbie [Level 2]

  • Kontributors
  • PipPip
  • 31 posts

Posted 21 September 2009 - 12:00 AM

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, 21 September 2009 - 12:01 AM.





Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users