Get date in iso-formatted

Hi,

I want to use an ESP32 to talk to an REST API.
For authentication, a need the current date/time in iso-format.
With the NTPClient, I can easily get the time formatted as "10:17:25" or "1672222645"
But I need it formatted as "2011-10-05T14:48:00.000Z"
It seems so simple, but I can't find a library or function to do this.

Any help is appreciated.

#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.print(timeClient.getFormattedTime());
  Serial.print(" - ");
  Serial.println(timeClient.getEpochTime()); 

  delay(1000);
}

You don't need external library.
You can format your string using strftime()

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.