C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-13-2008, 03:15 AM   #1
Why bbebfe is not bbebfe?
 
bbebfe's Avatar
 
Join Date: Nov 2008
Location: Earth
Posts: 27
Question Help, how to distill date from time_t value?

This is my code below:
Code:
time_t now = time(NULL);
printf("%s\n", ctime(&now));
Its output is something like "Sat Dec 18 12:38:38 1971". I only want the date portion to be outputted, i.e. Dec 18 1971. Can anybody help me!

Any help is appreciated!
bbebfe is offline   Reply With Quote
Old 11-13-2008, 03:18 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
strftime will take a struct tm and a format string (like printf sort of), which you can get by calling localtime or gmtime.

Or you could just use sprintf() with the data in struct tm, since it gives you year, month, day, etc.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 11-13-2008, 04:20 AM   #3
Why bbebfe is not bbebfe?
 
bbebfe's Avatar
 
Join Date: Nov 2008
Location: Earth
Posts: 27
Thank you so much!
I put my code here to help others who need this two.
Code:
	char date_buff[40];
	time_t time_value = time(NULL);
	struct tm* my_tm = localtime(&time_value);
	strftime(date_buff, sizeof(date_buff), "%b %d,%Y\n", my_tm); 
	printf("%s\n", date_buff);
NOTE: The returned pointer from localtime is a static variable, that means it might be overwritten by subsequent calls to ctime, gmtime, or localtime.
bbebfe is offline   Reply With Quote
Reply

Tags
date, time

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Advancing day by day until it matches a second date nhubred C++ Programming 1 05-30-2009 08:55 AM
Checking array for string Ayreon C Programming 87 03-09-2009 03:25 PM
Date program starts DOS's date jrahhali C++ Programming 1 11-24-2003 05:23 PM
CDate Class - handle date manipulation simply LuckY C++ Programming 5 07-16-2003 08:35 AM


All times are GMT -6. The time now is 10:34 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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