converting date from 7-Apr-05 to 04072005

Posted on 2005-04-07
Last Modified: 2010-04-01
Hi experts,

I am looking for a function converting date from 7-Apr-05 to 04072005. thanks
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 );
       printf("Bad date: '%s'\n", Line );

    LVL 16

    Expert Comment

    COleDateTime      datatimp;
    CString      strTimp = datatimp.Format(_T("%m%d%Y"));
    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 (   return "";
         m = mpos/4 + 1;
         istringstream isy(ascDate.substr(pos+5));
         isy >> y;  
         y = (y < 50)? 2000 + y : (y < 100)? 1900 + y : y;
         if ( return "";
         ostringstream oss;
         oss << setfill('0') << setw(2) << d << setfill('0') << setw(2) << m << setfill('0') << setw(4) << y;
         return oss.str();

    Regards, Alex

    LVL 39

    Expert Comment

    Should be

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

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

      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.

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now