how to convert the time format from time library

I have NTP time in my arduino code and the format output like this :Mon May 06 22:13:49 2019 Can I convert the format to be like this: 2019-05-09%2008:00:00 ?

my NTP code:

#include <ESP8266WiFi.h>
#include <time.h>

void setup() {
  Serial.begin(115200);
  delay(100);
 
  
  WiFi.begin(ssid, password); 


  configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  Serial.println("\nWaiting for time");
  while (!time(nullptr)) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("");
  
}

void loop() {

  time_t now = time(nullptr);
  Serial.println(ctime(&now));
  

  }
}

you have to extract specific time units from time_t, and format them for presentation in the format of your choice. the Time library does the extracting for you

sprintf is the optimum way to format data for presentation:

void ShowDateLocal(time_t LocalTime)                   // LCD 2 MST
{          
  sprintf(LCDDate, "%04d/%02d/%02d ", year(LocalTime), month(LocalTime), day(LocalTime));
  lcd2.setCursor(2,0); lcd2.print(LCDDate);  lcd2.print(monthShortStr(month(LocalTime)));
  lcd2.setCursor(13,1); lcd2.print(dayShortStr(weekday(LocalTime)));
}

time_t(LocalTime) is time_t(UTC), adjusted for DST & local time zone. the format is YEARMODA

the line that starts with sprintf generates a string that prints the year in DEC 4 digits; a /, the month in DEC padded to 2 digits if necessary, a /, and the day in DEC, padded to 2 digits if necessary

the d on %04d makes it display DECimal, 04 makes the year display 4 digits, the 0 in 04 makes it add 0s to the left to pad out shorter numbers, the end product of that line:

2019/05/09

you would want dashes: - where I used slashes: /

this, and the time display routine, require a variable declaration above void setup:

//  LCD Displays
char LCDTime[]   = "00000000";      // used by sprintf to format time displays
char LCDDate[]   = "0000000000";    // used by sprintf to format date displays

much better than many lines of "if hour <10 then add a zero to the front..."

the same function, for UTC time:

void ShowTimeUTC(time_t t)                            // LCD 1 UTC
{          
  sprintf(LCDTime, "%02d:%02d:%02d", hour(t), minute(t), second(t));
  lcd1.setCursor(0,1); lcd1.print(LCDTime); lcd1.print(" UTC");
}

Thanks for your valuable reply..but can i use Serial.print instead of LCD?

I don't know if sprintf works in serial monitor. I just cut code out of my current project rather than type in all that sprintf gibberish from memory

extracting hour(t), minute(t), second(t)); does work.

Of course it will. sprintf just fills in a char array. Serial inherits from Stream. Stream knows how to print from a char array.

Is there any good example i can follow it :smiley: sorry because I am still learning! :art:

I came back here to put my solution for this case maybe someone will need it in the future :slight_smile:

#include <ESP8266WiFi.h>
#include <TimeLib.h>         // for Date/Time operations
#include <WiFiUdp.h>         // for UDP NTP

#include <SPI.h>             // Serial Peripheral Interface


char ssid[] = "ssid";       //  your network SSID (name)
char pass[] = "password";       // your network password

// NTP Servers:
IPAddress timeServer(195, 186, 4, 101); // 195.186.4.101 (bwntp2.bluewin.ch)
const char* ntpServerName = "ch.pool.ntp.org";
const int timeZone = 3;     // Central European Time (summer time)

WiFiUDP Udp;
unsigned int localPort = 8888;  // local port to listen for UDP packets

void setup(void) {


  Serial.begin(115200);

  Serial.print("Connecting to wifi ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.print("IP number assigned by DHCP is ");
  Serial.println(WiFi.localIP());
  Serial.println("Starting UDP");
  Udp.begin(localPort);
  Serial.print("Local port: ");
  Serial.println(Udp.localPort());

  Serial.print("Using NTP Server ");
  Serial.println(ntpServerName);
  //get a random server from the pool
  WiFi.hostByName(ntpServerName, timeServer);
  Serial.print("NTP Server IP ");
  Serial.println(timeServer);

  Serial.println("waiting for sync");
  setSyncProvider(getNtpTime);
  setSyncInterval(60);

  /*
    time_t t = getNtpTime();
    setTime(t);
  */

  /*
    time_t t = getNtpTime();
    setTime(t);
  */



}



/*-------- NTP code ----------*/

const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets

time_t getNtpTime()
{
  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  return 0; // return 0 if unable to get the time
}

// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}

void loop() {
  digitalClockDisplay();
  
}
void digitalClockDisplay()  {

Serial.print(year());
Serial.print("-");
Serial.print(month());
Serial.print("-");
Serial.print(day());
Serial.print(" ");
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.println(" ");


delay(1000);


}






/*-------- Serial Debug Code ----------*/