get today's date in mm/dd/yyyy format

Discussion in 'C++' started by Abhijit, Sep 14, 2004.

  1. Abhijit

    Abhijit Guest

    hello everybody,

    I got a prob. I see a lot of date formats when I do a search in
    Google. But can any one give me some pointers / an example to extract
    today's date in C++ ? (formate of date: dd/mm/yyyy)

    Thanks in advance
     
    Abhijit, Sep 14, 2004
    #1
    1. Advertising

  2. "Abhijit" <> wrote in message
    news:...
    > hello everybody,
    >
    > I got a prob. I see a lot of date formats when I do a search in
    > Google. But can any one give me some pointers / an example to extract
    > today's date in C++ ? (formate of date: dd/mm/yyyy)
    >
    > Thanks in advance


    Untested code

    #include <time.h>
    #include <stdio.h>

    time_t t = time(0);
    tm* lt = localtime(t);
    char date[11];
    sprintf(date, "%02d/%02d/%04d", lt->tm_mday, lt->tm_mon + 1, lt->year +
    1900);

    john
     
    John Harrison, Sep 14, 2004
    #2
    1. Advertising

  3. Abhijit

    Sharad Kala Guest

    "Abhijit" <> wrote in message

    > hello everybody,
    >
    > I got a prob. I see a lot of date formats when I do a search in
    > Google. But can any one give me some pointers / an example to extract
    > today's date in C++ ? (formate of date: dd/mm/yyyy)


    Take a look at functions in <ctime>. Also check Boost's Date-time library.

    Sharad
     
    Sharad Kala, Sep 14, 2004
    #3
  4. Abhijit

    David Fisher Guest

    > > I got a prob. I see a lot of date formats when I do a search in
    > > Google. But can any one give me some pointers / an example to extract
    > > today's date in C++ ? (formate of date: dd/mm/yyyy)
    > >
    > > Thanks in advance

    >
    > Untested code
    >
    > #include <time.h>
    > #include <stdio.h>
    >
    > time_t t = time(0);
    > tm* lt = localtime(t);
    > char date[11];
    > sprintf(date, "%02d/%02d/%04d", lt->tm_mday, lt->tm_mon + 1, lt->year +
    > 1900);


    Or ...

    #include <ctime>
    #include <iostream>

    const int MAXLEN = 80;
    char s[MAXLEN];
    time_t t = time(0);
    cftime(s, "%D", &t);
    std::cout << s << '\n';

    David Fisher
    Sydney, Australia
     
    David Fisher, Sep 14, 2004
    #4
  5. "David Fisher" <> wrote in message
    news:dcw1d.782$...
    >> > I got a prob. I see a lot of date formats when I do a search in
    >> > Google. But can any one give me some pointers / an example to extract
    >> > today's date in C++ ? (formate of date: dd/mm/yyyy)
    >> >
    >> > Thanks in advance

    >>
    >> Untested code
    >>
    >> #include <time.h>
    >> #include <stdio.h>
    >>
    >> time_t t = time(0);
    >> tm* lt = localtime(t);
    >> char date[11];
    >> sprintf(date, "%02d/%02d/%04d", lt->tm_mday, lt->tm_mon + 1, lt->year +
    >> 1900);

    >
    > Or ...
    >
    > #include <ctime>
    > #include <iostream>
    >
    > const int MAXLEN = 80;
    > char s[MAXLEN];
    > time_t t = time(0);
    > cftime(s, "%D", &t);
    > std::cout << s << '\n';
    >
    > David Fisher
    > Sydney, Australia
    >


    Never heard of cftime, did you mean strftime? If so then %D is part of the
    C99 standard, though apparently widely supported.

    john
     
    John Harrison, Sep 14, 2004
    #5
  6. Abhijit

    David Fisher Guest

    John Harrison wrote:

    >> #include <ctime>
    >> #include <iostream>
    >>
    >> const int MAXLEN = 80;
    >> char s[MAXLEN];
    >> time_t t = time(0);
    >> cftime(s, "%D", &t);
    >> std::cout << s << '\n';
    >>

    >
    > Never heard of cftime, did you mean strftime? If so then %D is part of the
    > C99 standard, though apparently widely supported.


    Hmm ... I'm not sure how standard cftime() is, actually - but it wasn't a
    typo ...

    Using strftime() it would be:

    #include <ctime>
    #include <iostream>

    const int MAXLEN = 80;
    char s[MAXLEN];
    time_t t = time(0);
    strftime(s, MAXLEN, "%m/%d/%Y", localtime(&t));
    std::cout << s << '\n';

    ("%D" with strftime seems to only have 2 digits - ?)

    David Fisher
    Sydney, Australia
     
    David Fisher, Sep 14, 2004
    #6
  7. Abhijit

    Jon Bell Guest

    In article <>,
    John Harrison <> wrote:
    >
    >"Abhijit" <> wrote in message
    >news:...
    >>
    >> [...] can any one give me some pointers / an example to extract
    >> today's date in C++ ? (formate of date: dd/mm/yyyy)

    >
    >sprintf(date, "%02d/%02d/%04d", lt->tm_mday, lt->tm_mon + 1, lt->year +


    I thought he was asking about how to *read* a date from an input stream.

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Sep 14, 2004
    #7
  8. "Jon Bell" <> wrote in message
    news:ci6o1i$alr$...
    > In article <>,
    > John Harrison <> wrote:
    > >
    > >"Abhijit" <> wrote in message
    > >news:...
    > >>
    > >> [...] can any one give me some pointers / an example to extract
    > >> today's date in C++ ? (formate of date: dd/mm/yyyy)

    > >
    > >sprintf(date, "%02d/%02d/%04d", lt->tm_mday, lt->tm_mon + 1, lt->year +

    >
    > I thought he was asking about how to *read* a date from an input stream.
    >


    You could well be right. Assuming said date is held in a C string, then

    int day, month, year;
    sscanf(date, "%d/%d/%d", &day, &month, &year);

    does the trick, but with minimal error checking.

    john
     
    John Harrison, Sep 14, 2004
    #8
  9. Abhijit

    Matt Hurd Guest

    (Abhijit) wrote in message news:<>...

    > I got a prob. I see a lot of date formats when I do a search in
    > Google. But can any one give me some pointers / an example to extract
    > today's date in C++ ? (formate of date: dd/mm/yyyy)
    >

    In Boost's (www.boost.org) date time library you might use:

    date from_uk_string(const std::string&)

    Where the parameter is a delimited date string where with order
    day-month-year eg: 25-1-2002.

    Reference: http://www.boost.org/libs/date_time/doc/class_date.html

    Hope this helps,

    Matt Hurd
    www.hurd.com.au
     
    Matt Hurd, Sep 16, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q3liZXJMb3R1cw==?=

    Does CompareValidator work for the date in dd-mmm-yyyy format?

    =?Utf-8?B?Q3liZXJMb3R1cw==?=, Jun 10, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    15,864
    Peter Blum
    Jun 10, 2005
  2. Chu
    Replies:
    3
    Views:
    4,207
    Shawn
    Aug 15, 2006
  3. Ashraf Ansari

    convert MM/dd/yyyy format into dd/MM/yyyy

    Ashraf Ansari, Aug 30, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    794
    Alexey Smirnov
    Aug 30, 2007
  4. Gilles Ganault

    Converting DD MM YYYY into YYYY-MM-DD?

    Gilles Ganault, Aug 17, 2009, in forum: Python
    Replies:
    18
    Views:
    1,382
    Mark Tolonen
    Aug 20, 2009
  5. J P Singh
    Replies:
    4
    Views:
    289
    J P Singh
    Feb 10, 2004
Loading...

Share This Page