#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    15
    Rep Power
    0

    time...without seconds in C!!


    hi!
    well i had been able to figure out how to get time in hours and minutes..not the seconds.....
    the following code gives me time as...10:30:23...hh:mm:sec
    i want only hh:min
    can anyone sugest how i can do this
    #include <time.h>
    #include <stdio.h>

    #define SIZE 256

    int
    main (void)
    {
    char buffer1[SIZE];
    char buffer2[SIZE];
    time_t curtime;
    struct tm *loctime;

    /* Get the current time. */
    curtime = time (NULL);

    /* Convert it to local time representation. */
    loctime = localtime (&curtime);


    /* Print out the date and time in the standard format. */
    fputs (asctime (loctime), stdout);

    /* Print it out in a nice format. */
    strftime (buffer1, SIZE, " %x\n", loctime);
    printf("date: %s\n",buffer1);

    strftime (buffer2, SIZE, "%X", loctime);
    printf("time: %s\n",buffer2);

    return 0;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Check out the other format specifiers on the man page for strftime (or in the help file entry for strftime).

    abbridged
    %H The hour as a decimal number using a 24-hour clock
    (range 00 to 23).

    %I The hour as a decimal number using a 12-hour clock
    (range 01 to 12).

    %k The hour (24-hour clock) as a decimal number (range
    0 to 23); single digits are preceded by a blank.
    (See also %H.) (TZ)

    %l The hour (12-hour clock) as a decimal number (range
    1 to 12); single digits are preceded by a blank.
    (See also %I.) (TZ)

    %M The minute as a decimal number (range 00 to 59).

    %n A newline character. (SU)

    %O Modifier: use alternative format, see below. (SU)
    value, or the corresponding strings for the current
    locale. Noon is treated as `pm' and midnight as
    `am'.

    %P Like %p but in lowercase: `am' or `pm' or a corre_
    sponding string for the current locale. (GNU)

    %r The time in a.m. or p.m. notation. In the POSIX
    locale this is equivalent to `%I:%M:%S %p'. (SU)

    %R The time in 24-hour notation (%H:%M). (SU) For a
    version including the seconds, see %T below.

    %s The number of seconds since the Epoch, i.e., since
    1970-01-01 00:00:00 UTC. (TZ)

    %S The second as a decimal number (range 00 to 61).

    %t A tab character. (SU)

    %T The time in 24-hour notation (%H:%M:%S). (SU)
    There are lots more options that look interesting, so I recommend reading it yourself.

    BTW, thanks. I had never looked at strftime before.

IMN logo majestic logo threadwatch logo seochat tools logo