Thread: Formatting Date and Time

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    UK
    Posts
    2

    Formatting Date and Time

    I am a novice C programmer. I have been going around in circles all day with what seems like a simple problem.
    Basically what I am doing is reading a string from a virtual GPS Feed. I have managed to parse the data ie. I have "time", "Longtitude" and "latitude" etc and send out whatever data I choose to an lcd screen

    The data for "date" is given like this "040212" meaning today. "DDMMYY". So my question is...
    How do I take this and return say "04/02/2012"
    or even better "04 April 2012" to the LCD screen?

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Sample code:
    Code:
    char *input = "040212";
    char *months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    
    printf("%.2s/%.2s/20%.4s\n", &input[0], &input[2], &input[4]);
    printf("%.2s %s 20%.4s\n", &input[0], months[(input[2] - '0') * 10 + (input[3] - '0') - 1], &input[4]);
    Outputs:
    04/02/2012
    04 February 2012

  3. #3
    Registered User
    Join Date
    Apr 2012
    Location
    UK
    Posts
    2
    Quote Originally Posted by nonoob View Post
    Sample code:
    Code:
    char *input = "040212";
    char *months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    
    printf("%.2s/%.2s/20%.4s\n", &input[0], &input[2], &input[4]);
    printf("%.2s %s 20%.4s\n", &input[0], months[(input[2] - '0') * 10 + (input[3] - '0') - 1], &input[4]);
    Outputs:
    04/02/2012
    04 February 2012
    Thanks very much! Really appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xslt date formatting
    By maes in forum Tech Board
    Replies: 0
    Last Post: 09-27-2006, 07:17 AM
  2. Formatting string to time
    By gustavosserra in forum C++ Programming
    Replies: 6
    Last Post: 05-03-2004, 12:36 PM
  3. Replies: 3
    Last Post: 06-13-2003, 06:47 AM
  4. formatting a date
    By stevew2607 in forum C++ Programming
    Replies: 4
    Last Post: 06-07-2002, 01:08 PM
  5. Extracting and formatting today's date
    By Chewie in forum C Programming
    Replies: 4
    Last Post: 08-16-2001, 07:45 AM