# Setting date and time format (yyyy/mm/dd hh:mm:ss) using RTC

**URL:** <https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688>\
**Category:** Programming\
**Created:** [April 9, 2015, 4:09pm UTC](https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688 "2015-04-09T16:09:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 9, 2015, 4:09pm UTC](https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688/1 "2015-04-09T16:09:24Z")

</div>

Hi, all

I am newbie to arduino. I need your help to solve my problem. Well, I want to display date and time on serial monitor by using RTC and arduino due. But the problem is the format of date and time. I have tried to display date and time using this code. But this code give me an output like yyyy/m/d h:m:s. I need to edit the format like yyyy/mm/dd hh:mm:ss.

```cpp
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
void setup () {
Serial.begin(9600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
  Serial.println("RTC is NOT running!");
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime( __DATE__ , __TIME__ ));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print("0");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print("0");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.println();
delay(1000);
}

```

I need your help and suggestions. Thanks in advance.

Regards,  
Intan

---

<div class="post-metadata">

**Author:** ![knut\_ny](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/knut_ny/32/1013044_2.png) [@knut\_ny](https://forum.arduino.cc/u/knut_ny)\
**Post date:** [April 9, 2015, 4:19pm UTC](https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688/2 "2015-04-09T16:19:18Z")

</div>

first:  
char time\_buf[16]; // size big enough

then print to this buffer like:

sprintf(time\_buf,"%02u:%02u:%02u ", now.hour(),now.minute(),now.second());  
Serial.print(time\_buf);

---

<div class="post-metadata">

**Author:** ![econjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/econjack/32/1105255_2.png) [@econjack](https://forum.arduino.cc/u/econjack)\
**Post date:** [April 9, 2015, 5:14pm UTC](https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688/3 "2015-04-09T17:14:55Z")

</div>

Sometimes, if you don't use many of the _sprintf()_ features, you can save some memory replacing it with a simpler function. For example, the example using _sprintf()_ and just hard coding in some values looked like:

```cpp
void setup() {
  // put your setup code here, to run once:
  char str[16];  

  Serial.begin(115200);

  sprintf(str,"%02u:%02u:%02u ", 11,55,5);  
  Serial.println(str);
}
void loop() {}

```

and required 3616 bytes. Another alternative:

```cpp
void setup() {
  // put your setup code here, to run once:
  char str[16];  

  Serial.begin(115200);

  MakeTime(str, 11,55,5);
  Serial.println(str);
}

void loop() {}

void MakeTime(char *buff, int hour, int min, int sec)
{
  char temp[3];
  
  buff[0] = '\0';
  itoa(hour, temp, 10);
  if (hour < 10) {
    strcpy(buff, "0");
  } 
  strcat(buff, temp);
  strcat(buff, ":");
  
 if (min < 10) {
    strcat(buff, "0");
  } 
  itoa(min, temp, 10);
  strcat(buff, temp);
  strcat(buff, ":");
  
 if (sec < 10) {
    strcat(buff, "0");
  } 
  itoa(sec, temp, 10);
  strcat(buff, temp);
}

```

while it looks longer, only requires 2518 bytes, a savings of almost 1100 bytes. True, you'd have to write another function, _MakeDate()_, to supply the rest of your needs, but my guess is that you still end up saving some memory. If memory's not an issue, _sprintf()_ is pretty easy to use.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 10, 2015, 6:53am UTC](https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688/4 "2015-04-10T06:53:16Z")

</div>

Hi, all

I have updated my script, like this:

```cpp
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
void setup () {
Serial.begin(9600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
  Serial.println("RTC is NOT running!");
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime( __DATE__ , __TIME__ ));
}
}
void loop () {
DateTime now = rtc.now();

char dt[16];
char tm[16];

sprintf(dt, "%02d/%02d/%02d", now.year(),now.month(),now.day());
sprintf(tm, "%02d:%02d:%02d", now.hour(),now.minute(),now.second());

Serial.print(dt);
Serial.print(" ");
Serial.print(tm);
Serial.println();

delay(1000);
}

```

But i still have problems. The time in those script is set in am-pm, but i need to set it to 24 hours. and the time that i got is 7minutes slower than GMT+7. For example, it is 13:32:55 now, but the script print it as 01:25:55.  
Do you have any suggestions? Thanks in advance.

Regards,  
Intan

---

<div class="post-metadata">

**Author:** ![knut\_ny](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/knut_ny/32/1013044_2.png) [@knut\_ny](https://forum.arduino.cc/u/knut_ny)\
**Post date:** [April 10, 2015, 7:25am UTC](https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688/5 "2015-04-10T07:25:47Z")

</div>

Set your RTC to correct time

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 10, 2015, 7:28am UTC](https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688/6 "2015-04-10T07:28:20Z")

</div>

> knut\_ny:  
> Set your RTC to correct time

Hi, knut\_ny

Would you mind to tell me how to set it?

Thanks in advance

Regards,  
Intan

---

<div class="post-metadata">

**Author:** ![knut\_ny](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/knut_ny/32/1013044_2.png) [@knut\_ny](https://forum.arduino.cc/u/knut_ny)\
**Post date:** [April 10, 2015, 10:19am UTC](https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688/7 "2015-04-10T10:19:49Z")

</div>

Adafruit rtclib ??

- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

---

<div class="post-metadata">

**Author:** ![econjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/econjack/32/1105255_2.png) [@econjack](https://forum.arduino.cc/u/econjack)\
**Post date:** [April 10, 2015, 1:33pm UTC](https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688/8 "2015-04-10T13:33:36Z")

</div>

If you're trying to affect the AM/PM flag, you probably should look at:

[http://datasheets.maximintegrated.com/en/ds/DS1307.pdf](http://datasheets.maximintegrated.com/en/ds/DS1307.pdf)

Table 2, page 8, shows the Timekeeper register that controls most of what the clock does. Bit 5 of register 0x02 controls the AM/PM flag. Combine that with some previous posts on this Forum and you should be able to figure everything out.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:13pm UTC](https://forum.arduino.cc/t/setting-date-and-time-format-yyyy-mm-dd-hh-mm-ss-using-rtc/303688/9 "2021-05-05T21:13:41Z")

</div>

Closed on May 5, 2021, 9:13pm UTC


