Solved

# converting date from 7-Apr-05 to 04072005

Posted on 2005-04-07
210 Views
Hi experts,

I am looking for a function converting date from 7-Apr-05 to 04072005. thanks
0
Question by:justinY

LVL 22

Expert Comment

something like:

char Pat[50];  char Out[ 50];
char Line[50];  int Month, p, Mon, Yr;

strcpy( Line, "7-Apr-05" );

strcpy( Pat, "JanFebMarAprMayJunJulAugSepOctNovDec" );

p = sscanf(  "%d-%3s-%d", Line, &Day, &Mon, &Yr );
if ( p == 3 ) {
Month = 1 + ( strstr( Pat, Mon ) - Pat ) / 3;
sprintf( Out, "%2d%2d%4d", Month, Day, Yr + 2000 );
}
else

0

LVL 16

Expert Comment

COleDateTime      datatimp;
datatimp.ParseDateTime(_T("7-Apr-05"));
CString      strTimp = datatimp.Format(_T("%m%d%Y"));
0

LVL 39

Accepted Solution

Or that:

#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

string AscDate2NumDate(const string& ascDate)
{
static const string months = "-Jan-Feb-Mar-Apr-May-Jun-Jul-Aug-Sep-Oct-Nov-Dec";

int pos, mpos;
if ((pos = ascDate.find('-')) == string::npos) return "";
if ((mpos = months.find(ascDate.substr(pos, 4))) == string::npos)  return "";
int d, m, y;
istringstream isd(ascDate.substr(0, pos));
isd >> d;
if (isd.fail())   return "";
m = mpos/4 + 1;
istringstream isy(ascDate.substr(pos+5));
isy >> y;
y = (y < 50)? 2000 + y : (y < 100)? 1900 + y : y;
if (isy.fail()) return "";
ostringstream oss;
oss << setfill('0') << setw(2) << d << setfill('0') << setw(2) << m << setfill('0') << setw(4) << y;
return oss.str();
}

Regards, Alex

0

LVL 39

Expert Comment

Should be

oss << setfill('0') << setw(2) << m << setfill('0') << setw(2) << d << setfill('0') << setw(4) << y;
0

## Featured Post

Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and â€¦
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.