Example Code for Arduino - Get Network Time via WiFi

Last revision 2026/02/05

Hardware Preparation

Software Preparation

Arduino IDE

Sample Code

This demo is from CSDN blogger[Naisu Xu].

#include <WiFi.h>

const char *ssid = "********";    //WIFI Name
const char *password = "********"; //WIFI Password 

const char *ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 8 * 3600;
const int daylightOffset_sec = 0;

void printLocalTime()
{
    struct tm timeinfo;
    if (!getLocalTime(&timeinfo))
    {
        Serial.println("Failed to obtain time");
        return;
    }
    Serial.println(&timeinfo, "%F %T %A"); // Format output 
}

void setup()
{
    Serial.begin(115200);
    Serial.println();

    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
        delay(500);
        Serial.print(".");
    }
    Serial.println("WiFi connected!");

    // Get time from network time server and configure it 
    // When succeed, the chip will use RTC clock to keep the time updated 
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    printLocalTime();

    WiFi.disconnect(true);
    WiFi.mode(WIFI_OFF);
    Serial.println("WiFi disconnected!");
}

void loop()
{
    delay(1000);
    printLocalTime();
}

Other Supplementary Information

struct tm

struct tm {
	int tm_sec; // second, from 0-59;
	int tm_min; // minute, from 0-59;
	int tm_hour; // hour, from 0-23;
	int tm_mday; // day in a month, from 1-31;
	int tm_mon; // month, from 0-11;
	int tm_year; // year, = real year-1900;
	int tm_wday; // Week, from 0-6, 0 for sunday, 1 for monday, and so on; 
	int tm_yday; // Date in a year, from 0-365, 0 for January 1, 1 for January 2, and so on; 
	int tm_isdst; // Daylight Saving Time identifier, tm_isdst is positive when DST is implemented; tm_isdst is equal to 0 if DST is not implemented; tm_isdst() is negative for unknown situation 
};

struct tm Format Output

Formatting characters Output
%a Abbreviation for day of the week
%A Full name of the day of the week
%b Abbreviation for month
%B Full name for month
%c Time string for standard date
%C Last two digits of the year
%d Day of the month in decimal
%D Month/day/year
%e Day of the month in decimal in a two-character field
%F Year-month-day
%g The last two digits of the year, using a week-based year
%G Years, use week-based years
%h Abbreviated month name
%H 24 hour clock
%I 12 hour clock
%j Day of the year in decimal
%m Month in decimal
%M Minutes in decimal
%p Equivalent display of local AM or PM
%r Time in 12 hours
%R Display hours and minutes: hh:mm
%S Second in decimal
%t Horizontal tab
%T Display hours, minutes and seconds in hh:mm:ss format
%u Day of the week, Monday is the first day (values from 0 to 6, Monday is 0)
%U Week of the year, with first Sunday as the first day of week one (values from 0 to 53)
%V Week of the year, using a week-based year
%w Day of the week in decimal (values from 0 to 6, 0 for Sunday)
%W Week of the year, with first Monday as the first day of week one(values from 0 to 53)
%x Standard date string
%X Standard time string
%y Year in decimal without century (values from 0 to 99)
%Y Year in decimal with century
%z Time zone name, or return null if no time zone name is available

Was this article helpful?

TOP