473,657 Members | 2,587 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Date & Time In One Variable

compman9902
105 New Member
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
7 4941
sicarie
4,677 Recognized Expert Moderator Specialist
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
9,208 Recognized Expert Moderator Expert
Use the Win32 struct SYSTEMTIME in MSDN.

check this.
Jun 15 '07 #3
compman9902
105 New Member
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
9,208 Recognized Expert Moderator Expert
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
105 New Member
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
JosAH
11,448 Recognized Expert MVP
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
105 New Member
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

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

Similar topics

3
2179
by: Ed | last post by:
Hi there, My problem is the following: When I assign a custom formatted Date & Time to a Date variable it loses it’s formatting. Ex. 2005-06-07 15:46 now when I assign this to a variable of type Date or DateTime this becomes 2005/06/07 03:46:00 The same code work flawlessly on various other machines, thus I presume this is settings related. I know that the Time function in VB.NET reads from the system time, which you set in...
3
383
by: Ed | last post by:
Hi there, My problem is the following: When I assign a custom formatted Date & Time to a Date variable it loses it’s formatting. Ex. 2005-06-07 15:46 now when I assign this to a variable of type Date or DateTime this becomes 2005/06/07 03:46:00 The same code work flawlessly on various other machines, thus I presume this is settings related. I know that the Time function in VB.NET reads from the system time, which you set in...
4
2819
by: Terry | last post by:
I have a TextBox with a date such as 15/01/2006 which I want to cast into a variable as a short date 15/01/06, also I need to cast a time such as 07:30 A.M. into a variable as a short time. What is the best syntax for this please? Regards
1
1676
by: jonny | last post by:
Option Explicit Private mdTarget1 As Date Private mdTarget2 As Date ' called on form load Public Sub SetRecordStartTime() mdTarget1 = DateAdd("n", 60, Time) mdTarget2 = DateAdd("n", 120, Time)
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.