Solved

Create date with format MMDDYYhhmmss

Posted on 2004-03-24
5
731 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
Comment Utility
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
Comment Utility
to use these variables and functions you must include windows.h.

Brandon
0
 
LVL 4

Expert Comment

by:bdunz19
Comment Utility
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
Comment Utility
what about a 2 digit month?
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

9 Experts available now in Live!

Get 1:1 Help Now