//flex table opened by JP

Click to See Complete Forum and Search --> : Today's date in C++


nothing
12-02-2003, 07:47 PM
How do I put today's date into a string in the format i.e
Mon Dec 01 18:45:02 2003 in C++? Thank you very much.

fishybawb
12-02-2003, 08:02 PM
#include <time.h>

time_t dateTime;
time(&dateTime);
char *date = ctime(&dateTime);

nothing
12-02-2003, 08:27 PM
Could you explain that line by line please?

fishybawb
12-02-2003, 09:15 PM
OK...

1.)#include <time.h>

Just joking ;)

2.)time_t dateTime;

Declares a variable to store the date/time of type time_t. I think most compilers actually interpret this as a long int, but I'm not too sure.

3.)time(&dateTime);

Uses the "time" function to store the raw date/time info into the variable dateTime, which has to be passed by reference. The raw format is dependent on the compiler I think, but it's not in an easily readable format ie. years elapsed since 1900, number of days since Sunday etc.

4.)char *date = ctime(&dateTime);

The ctime() function formats the date/time to a useful format (the one you wanted) and the result is stored in a character array (string) via a char pointer.


:t

nothing
12-02-2003, 09:44 PM
How many times did I say "thank you" to you today? :p

I don't know nothing about pointers yet so the last line confuses me. I don't want to make you get angry but could you explain the last line again in a little more detail? :)

fishybawb
12-02-2003, 10:19 PM
:D

Pointers are quite a complex topic, but not too hard as long as you don't try to make things complicated. This is only *simplified*, get a good book and study the topic for a while.

Every variable that you've been using so far is stored at an address in memory. For simplicity's sake, these addresses are often taught at school/college as being an array of "boxes", each of which contains a value (the variable's contents). You can test this out with the "&" prefix to a variable - the "&" returns the variable's address in memory:


int aVariable = 10;
cout << &aVariable;


That will return the memory address of the integer variable aVariable as a hexidecimal. A pointer (a variable with a "*" prefix) is a type of variable used to hold the address of a similarly type variable. For example, int *aPointer could hold the address of an integer variable.

I won't go into pages of detail, it'd probably turn out as a book :)
At first you won't see the point of them, but they do come in useful for passing parameters to functions, and all kinds of other "exciting" things.

Back to the char *date part, you can see that *date is a pointer to a variable of the type "char". If you store a string in this kind of variable, char *date actually points to the first character in the string, and becomes much like a more flexible char array. Try this:


char *string = "Hello";
cout << string[0];


Here the string "Hello" is assigned to consecutive memory addresses, starting with the address "pointed" to by char *string. You can see the similarity with arrays in the second line - string[0] returns the value stored in the first memory "box" (the one pointed to by char *string), or "H", string[1] the contents of the address immediately after and so on.

I'll stop now in case you're getting my headache, but feel free to ask more questions if you like :) Like I said, take a look in a textbook and on online tutorials for a better understanding - I've seen whole books devoted to pointers alone, and most will at least provide a whole chapter on them :eek:

:t

nothing
12-03-2003, 07:49 AM
Once more I thank you. Your explanation was very nice and I think I got the point. I even messed with the code a little to make it more C++ like. Take a look and tell me what you think, please.


#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

#include <ctime>

int main()
{
time_t dateTime;
time(&dateTime);
string date = ctime(&dateTime);

cout << date << endl;

return 0;
}

fishybawb
12-03-2003, 10:36 AM
Lookin' good :cool:

You're right, half the time I find myself coding more in C than C++. That's what lack of sleep will do to you :D

nothing
12-04-2003, 07:50 AM
Hey, fishybawb. Is this a good idea to get the first three characters of the string date?


#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

#include <ctime>

int main()
{
time_t dateTime;
time(&dateTime);
string date = ctime(&dateTime);

cout << date << endl;

cout << date.substr( 0, 3 ) << endl;

return 0;
}


:t

fishybawb
12-04-2003, 09:53 AM
Yep, that's fine. That's the big advantage of using the string type, you get all kinds of handy methods built in to manipulate them :)