Turning YYYY to YY

Hi everyone

I just started to explore the TinyGPS++ library, which I am finding very useful. However, I am trying to convert the YYYY number (e.g.: “2014”) into YY (“14”). I’ve tried snprintf but haven’t had any luck. Does anyone have any examples that I could use to achieve this?

Thanks!

#include <TinyGPS++.h>
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(GPSBaud);
  Serial.println();
}

void loop()
{
  // Dispatch incoming characters
  while (Serial1.available() > 0)
    gps.encode(Serial1.read());

if (gps.date.isUpdated())
  {           
          //////////// Print the date & time...
          char datetime_string[18]; //make this big enough to hold the resulting string
          snprintf(datetime_string, sizeof(datetime_string), "%02d/%02d/%2d %02d:%02d:%02d", 
          gps.date.month(), gps.date.day(), gps.date.year(), gps.time.hour(), gps.time.minute(), gps.time.second());
          //buffer now contains a string like 02/22/13 05:03:06
          Serial.print(datetime_string);
          Serial.println(" ");   
  }
}

year - 2000 should keep you going until the next century.

Year % 100 will give you a number in the range 0 .. 99 which you can then print however you want.

problem is of course that the number is in a char array

  char year[5] = "2014";
  char yr[3];
  strcpy(yr, &year[2]);
  Serial.println(yr);

or in place (if the cahr array allows)

  char year[5] = "2014";
  strcpy(year, &year[2]);
  Serial.println(yr);

a memmove() should work too

char year[5] = "2014";
  Serial.println(&year[2]);

Seems simpler to me

AWOL: char year[5] = "2014";   Serial.println(&year[2]);

Seems simpler to me

even better! +1

Thank you guys so much for the tips. This makes total sense! And thanks for the tip on how to pass the YY value to a variable... this will come super handy :)