Print current date and time in a file

Posted on 2004-04-16
Last Modified: 2010-04-15

My C program printing data in a file. Now, I need to add current date and time in a specific format. How would I do it? Is there any library function for that?

Here is what I print in a file:


Now, I need to add current date and time in yymmdd hh:mm format.


fprintf(outfile,"namedatetime=michelle date time\n");

So, it should print in a file like this (current date & time)

namedatetime=michelle 040416 10:47

Name (michelle) is constant, so don't need to worry about it. I will put michelle in print statement .

I will be really appreciated if you can help me in this.

Question by:John22
  • 3
  • 3
LVL 12

Expert Comment

ID: 10843016
Hi John22,
Use strftime:

 #include <time.h>

     size_t strftime(const char *s, size_t  maxsize,  const  char
     *format, const struct tm *timeptr);

LVL 12

Expert Comment

ID: 10843055
Here is an example:

#include <time.h>

time_t now;
char timebuf[256];

strftime(timebuf,"%y%m%d %H%M",localtime(now));

fprintf(outfile,"namedatetime=michelle %s\n",timebuf);

LVL 22

Accepted Solution

NovaDenizen earned 100 total points
ID: 10843151
#include <time.h>


time_t now_secs;
struct tm now_breakout;
char buf[1024];
now_breakout = *(localtime(&now)); // or use localtime_r
strftime(buf, sizeof(buf), "%g%m%d %H:%M", &now_breakout);
fprintf(outfile, "namedatetime = michelle %s\n", buf);

Check the strftime man page if you want to change the format string.  For instance, if you want to avoid y2k issues and use a 4-digit year instead, replace the "%g" with "%G"

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

LVL 12

Expert Comment

ID: 10843200
> strftime(timebuf,"%y%m%d %H%M",localtime(now));

should read

> strftime(timebuf,"%y%m%d %H%M",localtime(&now)); localtime expects a pointer.
LVL 22

Expert Comment

ID: 10843346
There's a mistake in my format fields.  The %g and %G give you the ISO 8601 bizarro year instead of the calender year, which you get through %y and %Y.  

Author Comment

ID: 10844309
Thanks stefan73 and NovaDenizen.

I tried NovaDenizen code, but I got error. After I chaged time_t now_secs to time_t now and it worked fine.

I also tried stefan73. I also got error, but can't figure out. Here is the error:

cc -i test.c
"test.c", line 210: warning: improper pointer/integer combination: arg #1
"test.c", line 210: warning: improper pointer/integer combination: arg #2
"test.c", line 210: warning: argument #3 is incompatible with prototype:
        prototype: pointer to const char : "/usr/include/time.h", line 86
        argument : pointer to struct tm {int tm_sec, int tm_min, int tm_hour, int tm_mday, int tm_mon, int tm_year, int tm_wday, int tm_yday, i...
"test.c", line 210: prototype mismatch: 3 args passed, 4 expected
cc: acomp failed for test.c

Here is line 210:

strftime(timebuf,"%y%m%d %H%M",localtime(&now));

Thanks again.

LVL 22

Expert Comment

ID: 10844526
Don't forget to change my %g to %y.  It may give you the right answer now, but as soon as the financial year changes over it will give you the wrong answer.

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now