toString.ino 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <Wire.h>
  2. #include <RTClib.h>
  3. RTC_DS1307 rtc;
  4. void setup () {
  5. Serial.begin(57600);
  6. #ifndef ESP8266
  7. while (!Serial); // wait for serial port to connect. Needed for native USB
  8. #endif
  9. if (! rtc.begin()) {
  10. Serial.println("Couldn't find RTC");
  11. Serial.flush();
  12. while (1) delay(10);
  13. }
  14. if (! rtc.isrunning()) {
  15. Serial.println("RTC is NOT running, let's set the time!");
  16. // When time needs to be set on a new device, or after a power loss, the
  17. // following line sets the RTC to the date & time this sketch was compiled
  18. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  19. // This line sets the RTC with an explicit date & time, for example to set
  20. // January 21, 2014 at 3am you would call:
  21. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  22. }
  23. // When time needs to be re-set on a previously configured device, the
  24. // following line sets the RTC to the date & time this sketch was compiled
  25. // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  26. // This line sets the RTC with an explicit date & time, for example to set
  27. // January 21, 2014 at 3am you would call:
  28. // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  29. }
  30. void loop() {
  31. DateTime now = rtc.now();
  32. //buffer can be defined using following combinations:
  33. //hh - the hour with a leading zero (00 to 23)
  34. //mm - the minute with a leading zero (00 to 59)
  35. //ss - the whole second with a leading zero where applicable (00 to 59)
  36. //YYYY - the year as four digit number
  37. //YY - the year as two digit number (00-99)
  38. //MM - the month as number with a leading zero (01-12)
  39. //MMM - the abbreviated English month name ('Jan' to 'Dec')
  40. //DD - the day as number with a leading zero (01 to 31)
  41. //DDD - the abbreviated English day name ('Mon' to 'Sun')
  42. char buf1[] = "hh:mm";
  43. Serial.println(now.toString(buf1));
  44. char buf2[] = "YYMMDD-hh:mm:ss";
  45. Serial.println(now.toString(buf2));
  46. char buf3[] = "Today is DDD, MMM DD YYYY";
  47. Serial.println(now.toString(buf3));
  48. char buf4[] = "MM-DD-YYYY";
  49. Serial.println(now.toString(buf4));
  50. delay(1000);
  51. }