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

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

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
Asked:
justinY
  • 2
1 Solution
 
grg99Commented:
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
   printf("Bad date: '%s'\n", Line );

 
0
 
nonubikCommented:
COleDateTime      datatimp;
datatimp.ParseDateTime(_T("7-Apr-05"));
CString      strTimp = datatimp.Format(_T("%m%d%Y"));
0
 
itsmeandnobodyelseCommented:
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
 
itsmeandnobodyelseCommented:
Should be

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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