0

How do I format a time_t structure to something like this year-month-day h:m:s, I have already tried using ctime:

time_t t = time((time_t*) NULL); char *t_format = ctime(&t); 

but it doesn't give me the desired results

example : 2011-11-10 10:25:03. What I need is a string containing the result so I can write it to a file. Thanks

2 Answers 2

1

Use strftime from time.h like so:

char tc[256]; strftime(tc, 256, "%Y-%m-%d %H:%M:%S", tm); 
Sign up to request clarification or add additional context in comments.

3 Comments

Also call struct tm *tm = localtime(&t); first.
I got it and btw you can write "%Y-%m-%d %H:%M:%S" as "%F %r"
@onica No, %H:%M:%S is not the same as %r, but %T is.
1

http://pubs.opengroup.org/onlinepubs/009695399/functions/strftime.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.