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

Date & Time In One Variable

compman9902
100+
P: 105
Hello, thank you for looking at this post.
I need help with the date and time in a program. All I need to do is put the date and time into one variable and that would solve everything. When I say date and time, I mean something like "10/12/08,16:52:08". Thanks. Also, the variable it has to be put in is a string variable.
Here are my specs.:

Dev-C++ 4.9.9.2
Windows XP Service Pack 2

Thanks again.
Jun 15 '07 #1
Share this Question
Share on Google+
7 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hello, thank you for looking at this post.
I need help with the date and time in a program. All I need to do is put the date and time into one variable and that would solve everything. When I say date and time, I mean something like "10/12/08,16:52:08". Thanks. Also, the variable it has to be put in is a string variable.
Here are my specs.:

Dev-C++ 4.9.9.2
Windows XP Service Pack 2

Thanks again.
I was going to recommend creating your own 'Date' class where you could overload all the methods you wanted, but I can't imagine C++ not having something like that already built for it, so I did a Google search of "c++ date". That should get you started!
Jun 15 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Use the Win32 struct SYSTEMTIME in MSDN.

check this.
Jun 15 '07 #3

compman9902
100+
P: 105
Here is the thing, for both of these great posts...
I am a complete and utter nooby.
I really don't have any experience with structs and barley any with functions, so code examples would really be a great way to show me. Heck, even a code example of how to get the struct SYSTEMTIME into a variable in the format of "10/12/08,16:52:08" for October 12th, 2008 at 4:52 and 8 seconds.
Thanks again.
Jun 15 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Here is the thing, for both of these great posts...
I am a complete and utter nooby.
I really don't have any experience with structs and barley any with functions, so code examples would really be a great way to show me. Heck, even a code example of how to get the struct SYSTEMTIME into a variable in the format of "10/12/08,16:52:08" for October 12th, 2008 at 4:52 and 8 seconds.
Thanks again.
I did a Google on SYSTEMTIME and got 112,000 hits. The code samples you want are in MSDN and that is public on the Inrernet.
Jun 16 '07 #5

compman9902
100+
P: 105
I did a Google on SYSTEMTIME and got 112,000 hits. The code samples you want are in MSDN and that is public on the Inrernet.
Alright, I searched and came up with the following code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <Windows.h>
  7. #include <conio.h>
  8. using namespace std;
  9. int main()
  10. {
  11. time_t now=time(NULL);
  12. struct tm *tm=localtime(&now);
  13. printf("[(%d",tm->tm_mon + 1);
  14. printf("/%d",tm->tm_mday);
  15. printf("/%d)",tm->tm_year + 1900);
  16. printf(",%d",tm->tm_hour + 1);
  17. printf(":%d",tm->tm_min);
  18. printf(":%d]\n",tm->tm_sec);
  19. system("PAUSE");
  20. return 0;
  21. }
I does great for just displaying the time and date in the format I want, but I need help putting what it shows into a string variable. Thanks.
Jun 17 '07 #6

Expert 10K+
P: 11,448
Alright, I searched and came up with the following code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <Windows.h>
  7. #include <conio.h>
  8. using namespace std;
  9. int main()
  10. {
  11. time_t now=time(NULL);
  12. struct tm *tm=localtime(&now);
  13. printf("[(%d",tm->tm_mon + 1);
  14. printf("/%d",tm->tm_mday);
  15. printf("/%d)",tm->tm_year + 1900);
  16. printf(",%d",tm->tm_hour + 1);
  17. printf(":%d",tm->tm_min);
  18. printf(":%d]\n",tm->tm_sec);
  19. system("PAUSE");
  20. return 0;
  21. }
I does great for just displaying the time and date in the format I want, but I need help putting what it shows into a string variable. Thanks.
There are many time/date related functions available; I guess you want the
asctime function; it converts a struct tm to a string. Read the API docs here.

kind regards,

Jos
Jun 17 '07 #7

compman9902
100+
P: 105
There are many time/date related functions available; I guess you want the
asctime function; it converts a struct tm to a string. Read the API docs here.

kind regards,

Jos
Actually, I have just found another way, thank you for all of your kind help.
Jun 17 '07 #8

Post your reply

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