#1
  1. I am still learning
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Kuala Lumpur, Malaysia
    Posts
    249
    Rep Power
    13

    Making time in simple dd/mm/yyyy hh:mm:ss format


    can anyone help this newbie in building a string on current time in dd/mm/yyyy hh:mm:ss format?

    many thanks in advance.

    the closest i get is this:
    #include <time.h>

    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime (&rawtime);

    which give me the default "Mon Jul 28 23:04:54 2003\n\o".

    kindly advice.

    thanks.
    I have yet to find the right way to do it.

    www.genedavinci.com
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12
    hi if u look this question has a solution in the same forum. I sending you the link hope it helps
    http://forums.devshed.com/t60658/s.html
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    i wrote a header file that's pretty useful for this stuff, i'll post it for you later when i switch computers.
  6. #4
  7. I am still learning
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Kuala Lumpur, Malaysia
    Posts
    249
    Rep Power
    13
    wow. so many nasty code for that. boy... i miss php's date functions.... :(

    have to start reading now... thanks for the link, Raghuraman
    I have yet to find the right way to do it.

    www.genedavinci.com
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    The solution presented in the link wasn't too bad. There's also this solution:

    Code:
        time_t now;
        struct tm* nows;
        
        now = time(0);
        nows = localtime(&now);
        
        printf("%2.2d/%2.2d/%4.4d %2.2d:%2.2d:%2.2d\n", nows->tm_mon + 1,
            nows->tm_mday, nows->tm_year + 1900, nows->tm_hour,
            nows->tm_min, nows->tm_sec);
    The strftime function is really your friend though. It's defined in the time.h header file.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main(){
    	unsigned int d = 17, m = 11, y = 1981, date1, date2, month, date, year;
    	unsigned int th = 5, tm = 55, ts = 5, time, hours, minutes, seconds;
    	clrscr();
    	date1 = (y-1980)*512+(m*32)+d;
    	time = th*2048 + tm*32 + ts*2;
    	printf("Date  & Time in combined form = %u, %u", date, time);
    	year = 1980 + (date1 >> 9);
    	month = ((date1 << 7) >> 12);
    	date1 = ((date1 << 11) >> 11);
    	hours = (time >> 11);
    	minutes = ((time << 5) >> 10);
    	seconds = ((time << 11) >> 11)/2;
    	printf("\nDate in normal form = %u/%u/%u ", date1, month, year);
    	printf("\nTime in normal form = %u:%u:%u ", hours, minutes, seconds);
    	getch();
    }
  12. #7
  13. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244

    Try this (ANSI C)


    #include <stdio.h>
    #include <time.h>

    #define DATELEN 20

    int main(){
    struct tm *myTime;
    char chrDate[DATELEN];
    time_t mytm;

    time(&mytm);
    myTime = localtime(&mytm);
    strftime(chrDate, DATELEN, "%m/%d/%Y %H:%M:%S", myTime);
    printf("[%s]\n", chrDate);

    return 0;
    }

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  14. #8
  15. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    here's that header i was talking about. i wrote it a while ago, but i still use it occasionally for nicely formatted dates/times:
    Code:
    /*			my_time.h			**************/
    /*    my personal time conversion library  ***/
    
    #include <time.h>
    #include <string>
    
    struct time_freeze
    {
    	char	day[10];		// day of the week
    	int		date;			// the date
    	int		year;			// the year
    	char	month[10];		// month of the  year
    	int		hour;			// hour of the day in 12 hour time
    	int		minute;			// minute
    	int		second;			// second
    	char	am_or_pm[2];	// am or pm ? true is pm
    };		
    
    /* takes the garbled obnoxious time outputs from the system and prints out a formatted time **
    ** also takes a time_freeze structure by reference in case the user needs to output the		**
    ** time in a different manner i.e. win32 windows, the bool flag determines if printf used   **
    ** if the flag is true, then printf is used, else the structure is just filled				*/		
    
    void gimme_time(struct time_freeze &cur_time, bool use_printf)
    {
    	time_t my_time; tm *source_time; 
    
    	my_time = time(&my_time);			//get the time in seconds since format
    	source_time = localtime(&my_time);	//get teh time into structured format
    
    	// check if it's am or pm
    	if(source_time->tm_hour > 11)
    		strcpy(cur_time.am_or_pm, "PM");
    	else
    		strcpy(cur_time.am_or_pm, "AM");
    
    	// what day of the week is it
    	switch(source_time->tm_wday)
    	{
    	case 0:
    		strcpy(cur_time.day,"Sunday");
    		break;
    	case 1:
    		strcpy(cur_time.day,"Monday");
    		break;
    	case 2:
    		strcpy(cur_time.day,"Tuesday");
    		break;
    	case 3:
    		strcpy(cur_time.day,"Wednesday");
    		break;
    	case 4:
    		strcpy(cur_time.day,"Thursday");
    		break;
    	case 5:
    		strcpy(cur_time.day,"Friday");
    		break;
    	case 6:
    		strcpy(cur_time.day,"Saturday");
    		break;
    	}
    
    	// date of the month
    	cur_time.date = source_time->tm_mday;
    
    	// month of the year
    	switch(source_time->tm_mon)
    	{
    	case 0:
    		strcpy(cur_time.month, "January");
    		break;
    	case 1:
    		strcpy(cur_time.month, "February");
    		break;
    	case 2:
    		strcpy(cur_time.month, "March");
    		break;
    	case 3:
    		strcpy(cur_time.month, "April");
    		break;
    	case 4:
    		strcpy(cur_time.month, "May");
    		break;
    	case 5:
    		strcpy(cur_time.month, "June");
    		break;
    	case 6:
    		strcpy(cur_time.month, "July");
    		break;
    	case 7:
    		strcpy(cur_time.month, "August");
    		break;
    	case 8:
    		strcpy(cur_time.month, "September");
    		break;
    	case 9:
    		strcpy(cur_time.month, "October");
    		break;
    	case 10:
    		strcpy(cur_time.month, "November");
    		break;
    	case 11:
    		strcpy(cur_time.month, "December");
    		break;
    	}
    
    	// get the year
    	cur_time.year = source_time->tm_year + 1900;
    
    	// get the hour
    	if(source_time->tm_hour == 0)
    		cur_time.hour = 12;
    	else
    		if((source_time->tm_hour > 0)&&(source_time->tm_hour < 13))
    			cur_time.hour = source_time->tm_hour;
    	else
    		if((source_time->tm_hour > 12)&&(source_time->tm_hour < 24))
    			cur_time.hour = source_time->tm_hour - 12;
    
    	//get teh minute
    	cur_time.minute = source_time->tm_min;
    
    	//get the second
    	cur_time.second = source_time->tm_sec;
    	
    	// print results to STDOUT  if user wants that
    	if(use_printf){
    	printf("\n%02u:%02u:%02u%s on %s %s %u %u\n\n", cur_time.hour,	cur_time.minute,	cur_time.second,
    		cur_time.am_or_pm, cur_time.day,	cur_time.month,	cur_time.date,	cur_time.year);}
    }
  16. #9
  17. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    here is a simple little function that I use to get date/time/timestamps
    Code:
    #define MYDATE "%Y-%m-%d"
    #define MYTIME "%H:%M:%S" 
    #define TIMESTAMP "%Y-%m-%d-%H:%M:%S"
    
    char *CDateTime(const char *format) {
            time_t today;
            static char tbuf[30];
            today = time(NULL);
            strftime(tbuf, 30, format, localtime(&today));
            
            return (&tbuf[0]);
    }
  18. #10
  19. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    Tossing my 2 cents into this argument, when displaying date and time in general, it is probably best to use strftime with the %c format or %x %X. Why the %c or %x %X, you ask? That's because, it replaces the date and time with the locale's date/time format, so it increases portability, esp. if the program is going to be used by someone else who is used to seeing mm/dd/yyyy or yyyy-mm-dd (Notice the original poster asked for dd/mm/yyyy, but a lot of the solutions here follow other formats :)). See http://www.openbsd.org/cgi-bin/man.c...86&format=html for more on strftime formats.
    Code:
    #include <stdio.h>
    #include <time.h>
    int main(void)
    {
      #define MAXBUF 50
      char buf[MAXBUF];
      time_t now = time(NULL);
    
      strftime(buf, MAXBUF, "%c", localtime(&now));
      printf("%s\n", buf);
    
      strftime(buf, MAXBUF, "%x %X", localtime(&now));
      printf("%s\n", buf);
      return 0;
    }
    Of course, if you're not going to display date and time to the user, I usually prefer YYYYMMDDhhmmss format, because it is real easy to sort in order alphabetically :). Then again, that's a personal preference
    Last edited by Scorpions4ever; July 29th, 2003 at 11:13 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  20. #11
  21. I am still learning
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Kuala Lumpur, Malaysia
    Posts
    249
    Rep Power
    13
    gosh. i am overwhelmed. thanks for the replies ppl!! thanks!!
    I have yet to find the right way to do it.

    www.genedavinci.com

IMN logo majestic logo threadwatch logo seochat tools logo