Solved

Create date with format MMDDYYhhmmss

Posted on 2004-03-24
5
748 Views
Last Modified: 2007-12-19
I am trying to write data to a file with the name MMDDYYhhmmss.txt,
but cannot seem to store the date in a variable.  I am not sure where to start to get a date with that type of format.

Can someone help,

thanks,

jmsloan
0
Comment
Question by:jmsloan
  • 3
5 Comments
 
LVL 4

Expert Comment

by:bdunz19
ID: 10671498
declare a SYSTEMTIME variable then use GetLocalTime():

SYSTEMTIME stTime;

GetLocalTime(&stTime);

then use stTime.wDay, stTime.wMonth, ect.
0
 
LVL 4

Expert Comment

by:bdunz19
ID: 10671509
to use these variables and functions you must include windows.h.

Brandon
0
 
LVL 4

Expert Comment

by:bdunz19
ID: 10671575
CHAR      format[256];
INT      hour;
SYSTEMTIME stTime;
GetLocalTime(&stTime);

if(stTime.wHour > 12)
      hour = stTime.wHour - 12;
else
      hour = stTime.wHour;

sprintf(format, "%d/%d/%d %d:%d:%d", stTime.wMonth, stTime.wDay, stTime.wYear, hour, stTime.wMinute, stTime.wSecond);
printf("%s\n", format);
0
 
LVL 3

Author Comment

by:jmsloan
ID: 10671851
what about a 2 digit month?
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 10671967
You can use strftime a little easier, and also across platforms:
char var[30] = {0}; // Variable that will hold the data
time_t now = time(NULL);
struct tm *tm_data = localtime(&now);
strftime(var, 30, "%m%d%y%H%M%S", tm_data);

You need to #include <time.h> or <ctime> for these functions.  The strftime documentation has information about all kinds of different modifiers (am/pm, 4 digit year, etc) to use with the date.  Good luck.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 169
C++ question 3 62
C++ error cannot convert from std::string to const char * 6 130
C++ :Change value from  DisableCMD registry 4 51
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now