Extracting and formatting today's date

This is a discussion on Extracting and formatting today's date within the C Programming forums, part of the General Programming Boards category; I would like to display today's date in the format dd/mm/yyyy. What is the best function to use to do ...

  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,600
    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:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21