[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 255

# converting date from 7-Apr-05 to 04072005

Hi experts,

I am looking for a function converting date from 7-Apr-05 to 04072005. thanks
0
justinY
• 2
1 Solution

Commented:
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

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

Commented:
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

Commented:
Should be

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

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.