By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

date formating

P: 10
Hi all,

the following is a chunk of my code:

char timebuf[10];
char datebuf[10];
_strtime( timebuf );
_strdate( datebuf );

cout<<timebuf<<endl; //output: 15:48:59
cout<<datebuf<<endl; //output: 09/14/06

what is the simplest way to format the date to look like 09-14-2006

PS:I have all the necessary libraries included.

Thanks a lot
Sep 14 '06 #1
Share this Question
Share on Google+
3 Replies


P: 61
There is no function to change the date to the format you required. Just access the position of colon in the buffer datebuf and change the colon to hyphen.
Sep 15 '06 #2

P: 10
Ok i just did simple array formatting however it was giving me casting problems when i was jsut trying to do...

datebuf[2] = "-"; // why was this causing the following issue?

error C2440: '=' : cannot convert from 'char [2]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

Finally i decided to go through ASCII unable to resolve the above problem:

char timebuf[10];
char datebuf[10];
const unsigned char asciiDash(45);
_strtime( timebuf );
_strdate( datebuf );
datebuf[2] = asciiDash;
datebuf[5] = asciiDash;

and got 09-15-06...
Sep 15 '06 #3

P: 61
See the assignment you are done......

datebuf[2] = "-"; // why was this causing the following issue?


Lefthand side is a character and the right hand side is a string which is of type (char *). So the RHS is not compatible with the LHS. Hence the issue.

See the difference between single quotes and double quotes.

Make the assignment like this.....

datebuf[2] = '-';
Sep 15 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.