How to display date ?

Dec 15, 2008 at 8:41am
How can i just display the current Date WITHOUT displaying the time ?

Thank You.
Dec 15, 2008 at 9:05am
1
2
3
char date[9];

_strdate(date);





in time.h, I think.
Dec 15, 2008 at 4:53pm
I tried that out and i added

cout<<date;

It didn't work. No output.
Last edited on Dec 15, 2008 at 4:54pm
Dec 15, 2008 at 4:56pm
Is your date a class object?
If so, look for some of its members
Last edited on Dec 15, 2008 at 4:57pm
Dec 16, 2008 at 7:13am
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 Dec 16, 2008 at 7:14am
Dec 16, 2008 at 7:23am
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 . . .
Dec 16, 2008 at 7:42am
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
Dec 16, 2008 at 8:30am
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 Dec 16, 2008 at 8:31am
Dec 16, 2008 at 8:57am
Nope. Still doesn't show any output.

BTW to let u know the cursor jumps like 10 lines and the program closes.
Dec 16, 2008 at 9:21am
And this is when you do this isolated little program alone? I.e., just the 9 lines of my previous post?
Dec 17, 2008 at 7:23am
Every code posted in this thread produces the same strange blank output. The cursor jumps some 10 lines and the program closes.
Dec 17, 2008 at 7:35am
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).

Dec 17, 2008 at 7:57am
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);
Dec 17, 2008 at 4:20pm
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 Dec 17, 2008 at 4:23pm
Dec 17, 2008 at 4:34pm
Post exactly what you have for code and we'll try to help you
Dec 17, 2008 at 4:36pm
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 Dec 17, 2008 at 4:40pm
Dec 17, 2008 at 6:36pm
Oops.
Topic archived. No new replies allowed.