Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Create date with format MMDDYYhhmmss

Posted on 2004-03-24
5
768 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

837 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