Thread: How do i store today's date in variable?

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    Post How do i store today's date in variable?

    Hi all !

    im working on project currently (console). Its a program that needs to print reports at some point along with the date that the report has been created.

    So my question is.. how do i store the date in a variable in any of the usual formats (like dd-mm-yy or dd month yy ..etc) so i can put them in the report?? if you could also show how to print the time i'd appreciate it..


    Note :
    i've searched the C board for threads on this topics but most of them were complicated or don't show what kind of arguments some functions should take.



    thanks in advace
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read about
    strftime()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Thanks Salem.

    I've found some sites that shows the arguments of strftime() but none of them illustrates how to use it. Will someone please show me how to use this function in a small example?


    appreciate your help.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    #include <time.h>
    
    time_t current = time(NULL); /* Gets GMT time as a time_t. */
    struct tm* pLocal = localtime(&current); /* Converts to local time in broken down format. */
    strftime(buffer, sizeof(buffer), "%d %B %Y", pLocal); /* Formats as string into provided buffer. */
    This format string will print: 22 August 2004

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Brain Cell
    Will someone please show me how to use this function in a small example?
    Here and here.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
    FILE *mystream;
    
    char buffer[50];
    
    time_t current = time(NULL);
    struct tm* pLocal = localtime(&current); 
    strftime(buffer, sizeof(buffer), "Report created %a %x at %X %p", pLocal);
    
    mystream = fopen("output.txt", "w+");
    
    fprintf(mystream , "------------------------------------------------\n"
                       "%s\n"
                       "------------------------------------------------"
                       , buffer);
    fclose(mystream);
    
    system("PAUSE");
      return 0;
    }
    i studied anonytmouse's code modified and add to it a bit to make exactly what i wanted.

    thanks alot guys. I never knew its gonna be as easy as this.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Just as a note, you should check the return value from the fopen() before using mystream

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    My output:
    Code:
    ------------------------------------------------
    Report created Sun Sun Aug 22 at 22:05:12 PM
    ------------------------------------------------
    Last edited by Dave_Sinkula; 08-22-2004 at 09:08 PM. Reason: colorization
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Thantos :
    yea but i was trying to show the concept only

    Dave_Sinkula :
    what? im getting this output :
    Code:
    ------------------------------------------------
    Report created Mon 08/23/04 at 07:47:20 AM
    ------------------------------------------------
    why are you getting a different output?? and how do i let my program prints the same output i got on every computer??
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    why are you getting a different output?? and how do i let my program prints the same output i got on every computer?
    The reason for the difference is because the output is based off of the locale of the machine is running on.

    Try this instead as it relies less on the locale
    Code:
    strftime(buffer, sizeof(buffer), "Report created %a %m/%d/%Y at %I:%M:%S %p", pLocal);
    Last edited by Thantos; 08-22-2004 at 11:07 PM.

  11. #11
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    k thanks Thantos.

    And maybe someone else can try it and tell us what he gets.

    mine is :
    Code:
    ------------------------------------------------
    Report created Mon 08/23/2004 at 09:36:35 AM
    ------------------------------------------------
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  12. #12
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Also,
    in case you are using gcc, you can issue:
    Code:
    printf("Today is: %s\n",__DATE__);
    printf("Time now is: %s\n",__TIME__);
    Im not sure if this is portable...

    -Harsha.
    Help everyone you can

  13. #13
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    __DATE__ and __TIME__ are the date and time of compilation, not run. They are ansi standard.
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  3. Beginner question
    By Tride in forum C Programming
    Replies: 30
    Last Post: 05-24-2003, 08:36 AM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM
  5. Incrementing time and calendarfrom 1999-2000
    By Karma in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2001, 02:48 PM