我在Arduino代码和ESP8266开发板上使用NTP服务器。我正在使用“ Serial.println(timeClient.getFormattedTime());”但是该功能仅打印时间而不是日期(DD-MM-YYYY),我需要日期。.
这是我的代码:
#include <NTPClient.h>
// change next line to use with another board/shield
#include <ESP8266WiFi.h>
//#include <WiFi.h> // for WiFi shield
//#include <WiFi101.h> // for WiFi 101 shield or MKR1000
#include <WiFiUdp.h>
const char *ssid = "----------";
const char *password = "**********";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
void loop() {
timeClient.update();
Serial.println(timeClient.getFormattedTime());
delay(1000);
}
谢谢
如果查看该库的源代码,将会看到所有getFormattedtime函数应该输出。
我没有在该库中看到该日期的函数。在我看来,您必须使用getEpochTime获取纪元时间并自己计算日期。互联网上应该有很多关于如何将纪元时间转换为日期的示例。
时间库具有将Unix时间转换为月份和年份的功能。