How to display date ?

How can i just display the current Date WITHOUT displaying the time ?

Thank You.
1
2
3
char date[9];

_strdate(date);





in time.h, I think.
I tried that out and i added

cout<<date;

It didn't work. No output.
Last edited on
Is your date a class object?
If so, look for some of its members
Last edited on
No my date is not a class object.

Actually i am designing a movie ticket booking program and i need the program to display the current date , tomorrow's date and day after tomorrow's date so that the customer can decide on which day he wants the ticket to be booked.

I further want that date to be printed into the ticket which is stored in a text file along with other information about his ticket.

So that's the aim. Anyone can help ?
Last edited on
The code posted by schyte works.

Program:

1
2
3
4
5
6
7
8
9
10
11
#include<time.h>
#include<iostream>
using namespace std;

int main()
{
	char date[9];
	_strdate(date);
	cout << date << endl;
}


output:

1
2
12/16/08
Press any key to continue . . .
Hmm.. I am using Borland Compiler 5.5 (so no 'using namespace std;')and my OS is windows XP (no SP). Yes i tried it out and it still isn't working.

T.T
My bad. I'm using Visual Studio 9.0. I would think your namespace problem is only related to cout and endl, not to the use of _strtime(). To avoid using iostream's output functions, try using printf() instead:

1
2
3
4
5
6
7
8
9
#include<time.h>
#include<stdio.h>

int main()
{
	char date[9];
	_strdate(date);
	printf("Date: %s\n", date);
}


Let me know how it works out...
Last edited on
Nope. Still doesn't show any output.

BTW to let u know the cursor jumps like 10 lines and the program closes.
And this is when you do this isolated little program alone? I.e., just the 9 lines of my previous post?
Every code posted in this thread produces the same strange blank output. The cursor jumps some 10 lines and the program closes.
Ok i tried out this code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream.h>
#include<conio.h>
#include<dos.h>

void main()
{
	clrscr();
	
	date d1;
	getdate(&d1);
	cout<<d1.da_year;
	
	getch();
}


It shows the current year alright. But when i add this statement :

cout<<d1.da_year<<d1.da_mon<<d1.da_day;

I get 2008** where * and * are random garbage values (of da_mon and da_day maybe).

What is all this? Can't anyone provide a solution that uses just the standard library? I damn you to Hell, Borland!

1
2
3
4
5
6
7
8
time_t secs=time(0);
tm *t=localtime(&secs);
//This prints the date in ISO format.
printf("%04d-%02d-%02d\n"t->tm_year+1900,t->tm_mon+1,t->tm_mday);
//This prints the date and time in ISO format.
printf("%04d-%02d-%02d %02d:%02d:%02d\n",
	t->tm_year+1900,t->tm_mon+1,t->tm_mday,
	t->tm_hour,t->tm_min,t->tm_sec);
Err i can't seem to compile that code as the compiler complains of a missing function call ')' in function main() at line no. 4

But yea i resolved it using cout statements. Thanks a lot it works
Last edited on
Post exactly what you have for code and we'll try to help you
I think the problem was a missing comma
printf("%04d-%02d-%02d\n",t->tm_year+1900,t->tm_mon+1,t->tm_mday);
Last edited on
Oops.
Topic archived. No new replies allowed.