Thread: Extracting and formatting today's date

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    2

    Question Extracting and formatting today's date

    I would like to display today's date in the format dd/mm/yyyy. What is the best function to use to do so? I know that the time() function will give me number of seconds since Jan.1,1970, but do I have to parse the rest of the info. out from that or is there an easier way/function to use??

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you can use the function asctime(const struct tm* pointer)

    This returns a pointer to a string which contains the information that you need ( and a little more besides). You then have to break that string down a little to do what you want.

    cant remember the exact format of the string, its something like

    day/date/month/time/year /0 or something similar. look it up in your compiler helpfiles.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Try this:

    int tm_year;
    struct tm *systime;
    time_t t;
    t=time(NULL);
    systime=localtime(&t);

    if(systime->tm_hour>12)
    {systime->tm_hour=systime->tm_hour-12; }
    ///so output isn't military format///

    printf("Today is %.2d/ %.2d/ %.2d !!\n ",
    systime->tm_mon+1,
    systime->tm_mday,
    systime->tm_year+1900);

    ///"systime" is a structure defined in time.h
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    2

    Thanx Sebastiani, for helping me see the light!

  5. #5
    Unregistered
    Guest

    Time

    #include <stdio.h>
    #include <time.h>
    int main() {
    time_t t;

    time(&t);
    printf(ctime(&t));

    system("PAUSE");
    return 0;
    }



    Sall

Popular pages Recent additions subscribe to a feed