Getting Date & Time From NTP Server With ESP32
Maybe your like
Imagine building a smart device that turns on your bedroom lamp exactly at 7:00 AM, or a weather station that logs temperature data every 15 minutes. Maybe you’re creating your own internet-connected clock that always shows the exact time, down to the second. In all of these projects, one thing becomes crystal clear—keeping accurate time is absolutely essential.
That’s where NTP, or Network Time Protocol, comes in. NTP is an internet protocol that lets your ESP32 (or any computer or device) sync its clock with extremely precise time servers. And the best part is that it’s completely free, and you don’t need to connect any extra hardware. All you need is an internet connection.
In this tutorial, you’ll learn what NTP is, how it works, and how to use it with your ESP32 to get the exact current time.
Let’s get started!
Wait, Doesn’t the ESP32 Already Have a Built-in Clock?
You might be wondering, “The ESP32 has a built-in RTC, so why do I need NTP at all?” That’s a great question!
Yes, the ESP32 does come with an internal RTC module. It can keep track of hours, minutes, and seconds even while it’s doing other tasks. But there’s a catch.
- When your ESP32 first powers on, its internal clock has no idea what the actual time is. It just starts from zero unless you tell it the correct time yourself.
- Also, unlike some external RTC modules (such as the DS1307 or DS3231) that have battery backup, the ESP32’s RTC doesn’t remember the time when it loses power or resets.
- And even if you keep the ESP32 running continuously, its internal clock can slowly drift and become less accurate over time. After a few days or weeks, it might be off by several seconds or even minutes.
This is why NTP is so helpful. It allows the ESP32 to get the real-world time from special NTP servers on the internet that are synchronized with atomic clocks—some of the most accurate clocks in the world. If your ESP32 resets or loses power, it can simply ask the NTP server for the correct time again. And if its internal clock starts to drift, the ESP32 can re-sync with the NTP server from time to time to correct it and stay accurate.
How Does NTP Actually Work?
NTP uses a hierarchical system called “Strata” (the plural of stratum). Each stratum has a value associated with it that is used to identify how accurate a clock is. These values range from 0 to 15, where 0 is the most accurate and 15 is the least accurate. Anything above 15 means the clock isn’t trustworthy at all.

At the top of the hierarchy is Stratum 0. These are not computers, but highly accurate timekeeping devices like atomic clocks, GPS satellites, or radio clocks. Stratum 0 devices don’t connect directly to the internet. Instead, they are connected to Stratum 1 servers. These are the primary time servers that get their time straight from Stratum 0 devices.
Stratum 2 servers get their time from Stratum 1 servers, and Stratum 3 servers get their time from Stratum 2, and so on. As you move down the hierarchy, the time becomes slightly less accurate due to network delays, but it’s still precise enough for most everyday uses.
Your personal computer or a typical web server usually connects to a Stratum 2 or Stratum 3 server to get its time.
How Does an ESP32 Get Time Using NTP?
Let’s look at a real example of how an ESP32 microcontroller gets the current time and date using NTP.
First, we need a time source. For small projects, that source is usually an internet-based NTP server. One of the most popular options is pool.ntp.org. This is a free, open project that connects your device to a network of time servers from around the world.
There are also regional versions of the NTP pool designed for different parts of the world, such as:
| Area | HostName |
| Worldwide | pool.ntp.org |
| Asia | asia.pool.ntp.org |
| Europe | europe.pool.ntp.org |
| North America | north-america.pool.ntp.org |
| Oceania | oceania.pool.ntp.org |
| South America | south-america.pool.ntp.org |
When your device connects to pool.ntp.org, it automatically tries to find a server that is both reliable and fast. However, if you know where your device is located, using a regional server can give you better results, because these servers are geographically closer to you, which means they can respond faster. This helps your ESP32 sync its time more quickly and accurately.
When the ESP32 (which acts as a client) wants to get the time, it sends a request to an NTP server using something called the User Datagram Protocol (UDP). Unlike TCP, which constantly checks to make sure data arrived correctly, UDP is faster and more lightweight, which is ideal for quick time checks. NTP specifically uses UDP port 123.

The server responds by sending back a data packet containing the current time in Coordinated Universal Time (UTC). UTC is a global time standard, very similar to Greenwich Mean Time (GMT). The important thing about UTC is that it does not change with time zones or daylight saving; it stays the same everywhere in the world.
Once the ESP32 receives the UTC time, it adjusts its own internal clock. Then, if needed, it applies any local time zone offset (like converting to Eastern Time or Pacific Time) or daylight saving time adjustments.
Once the ESP32 syncs with the NTP server, it keeps track of time using its internal Real-Time Clock (RTC). It doesn’t need to request the time every second. Instead, it re-syncs periodically, maybe once an hour or once a day, to prevent the clock from drifting too far off.
Setting Up the Arduino IDE
Now that you understand how NTP works and how the ESP32 can use it, it’s time to see how we can program the ESP32 to actually get the current date and time from an NTP server.
We will be using the Arduino IDE to program the ESP32, so please ensure you have the ESP32 add-on installed before you proceed:
Installing ESP32 Board in the Arduino IDEThe ESP32 microcontroller has quickly become one of the most popular boards among hobbyists, engineers, and people interested in the Internet of Things (IoT). It’s... Example Code
The example below shows you how to get the current date and time from an NTP server using your ESP32. Copy the code into your Arduino IDE, but don’t upload it just yet! There are a few important changes you need to make first to ensure everything works correctly for your setup.
First, you need to tell the ESP32 how to connect to your Wi-Fi network. Find these two lines in the code and replace the placeholder text with your actual network name and password:
// Replace with your network credentials const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD";Next, you need to set the correct UTC offset for your time zone. Remember, NTP servers provide the time in UTC. This is a standard time used all over the world and does not change based on location. Since you probably don’t live in the UTC time zone, you need to give the ESP32 an offset to help it adjust to your local time. Your local time zone is a certain number of hours ahead of or behind UTC.
The offset must be written in seconds. Here’s how to calculate it: multiply the number of hours by 60 (minutes) and then by 60 again (seconds). For example:
- If you’re in Eastern Standard Time (UTC -5), your offset is: -5 × 60 × 60 = -18000
- If you’re in Central European Time (UTC +1), your offset is: 1 × 60 × 60 = 3600
- If you’re at UTC +0 (like London), your offset is: 0 × 60 × 60 = 0
You can find a complete list of UTC time offsets here to look up your specific time zone.
You also need to set the daylight saving time offset. If your country or region observes daylight saving time, set this value to 3600 (which equals 1 hour). If your region doesn’t use daylight saving time, just set it to 0.
const int daylightOffset_sec = 3600;
After making all these changes, you’re ready to upload the code to your ESP32!
// Libraries to get time from NTP Server #include <WiFi.h> #include <time.h> // Replace with your network credentials const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD"; // NTP server setup const char* ntpServer = "pool.ntp.org"; const long gmtOffset_sec = -18000; // Adjust this for your timezone const int daylightOffset_sec = 3600; // Adjust if DST is in effect // Function that prints formatted date and time void printDateTime() { struct tm timeinfo; if (!getLocalTime(&timeinfo)) { Serial.println("Failed to obtain time"); return; } char formattedTime[80]; // Buffer to store the formatted string strftime(formattedTime, sizeof(formattedTime), "%A, %B %d %Y %H:%M:%S", &timeinfo); Serial.println(formattedTime); } void setup() { Serial.begin(115200); // Connect to WiFi WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println("\nConnected to WiFi!"); // Configure NTP configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); Serial.println("NTP time configured."); } void loop() { // Print formatted date and time printDateTime(); delay(1000); }Demonstration
After uploading the code, open the Serial Monitor in Arduino IDE and make sure the baud rate is set to 115200. Then press the EN (reset) button on your ESP32. If everything is set up correctly, you should see the current date and time being printed every second on the Serial Monitor.

Code Explanation
Let’s take a quick look at the code to see how it works.
The code starts by including two important libraries. The WiFi.h library helps the ESP32 connect to Wi-Fi. The time.h library allows the ESP32 to communicate with the NTP server and format the time into a readable format.
#include <WiFi.h> #include "time.h"Next, we define two variables for your Wi-Fi connection: one for the network name (SSID) and one for the password. This step is very important because the ESP32 needs internet access to reach the NTP server and get the correct time. That’s why you had to replace the placeholder text with your actual network credentials earlier.
const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASS";After that, we configure the NTP settings. We choose a time server, in this case, “pool.ntp.org”, and tell the ESP32 how to adjust from UTC to your local time zone.
We use gmtOffset_sec to shift the time to your time zone and daylightOffset_sec to add an extra hour if your location observes daylight saving time. For example, a gmtOffset_sec of -18000 represents UTC-5 (Eastern Standard Time), and a daylightOffset_sec of 3600 adds one hour for daylight saving.
It’s important to get these values right! If they’re incorrect, your timestamps will be wrong even though the NTP connection is working perfectly.
const char* ntpServer = "pool.ntp.org"; const long gmtOffset_sec = -18000; const int daylightOffset_sec = 3600;As mentioned earlier, “pool.ntp.org” is a good default server because it automatically connects you to a fast and reliable time server. However, if you prefer, you can use a regional server like “europe.pool.ntp.org” or “asia.pool.ntp.org” depending on your location.
In the setup section, we first establish serial communication with the computer and then we try to connect the ESP32 to the Wi-Fi network. We use the WiFi.mode() function to put the Wi-Fi radio in station mode and the WiFi.begin() function to actually start the connection process. While it’s connecting, you’ll see dots appearing in the Serial Monitor. Once connected, you’ll see a “Connected to WiFi!” message.
Serial.begin(115200); // Connect to WiFi WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println("\nConnected to WiFi!");Once the ESP32 is connected to the network, we call the configTime() function. This function syncs the ESP32’s internal clock with the NTP server using the time zone offset, daylight saving setting, and NTP server address we configured earlier.
Once that’s done, the ESP32 knows the correct date and time and will keep track of it even if the Wi-Fi disconnects using its internal clock.
// Configure NTP configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); Serial.println("NTP time configured.");We then have a custom function called printDateTime(). This function prints the current date and time to the Serial Monitor in a nice, readable format. First, it asks the ESP32’s internal clock for the current time (which was set earlier using the NTP server). Then it formats that information into a human-readable string like “Wednesday, December 03 2025 15:42:01” using a built-in function called strftime(). If something goes wrong and it can’t get the time, it prints an error message instead.
void printDateTime() { struct tm timeinfo; if (!getLocalTime(&timeinfo)) { Serial.println("Failed to obtain time"); return; } char formattedTime[80]; // Buffer to store the formatted string strftime(formattedTime, sizeof(formattedTime), "%A, %B %d %Y %H:%M:%S", &timeinfo); Serial.println(formattedTime); }The format string uses various codes (called specifiers) that start with the % symbol to represent different parts of the date and time.
Here is a list of specifiers you can use to customize how the date and time are displayed. For example:
| %a | Abbreviated weekday name (e.g., “Sun”) |
| %A | Full weekday name (e.g., “Sunday”) |
| %b | Abbreviated month name (e.g., “Jan”) |
| %B | Full month name (e.g., “January”) |
| %d | Day of the month as a decimal number (01-31) |
| %H | Hour (24-hour clock) as a decimal number (00-23) |
| %I | Hour (12-hour clock) as a decimal number (01-12) |
| %M | Minute as a decimal number (00-59) |
| %S | Second as a decimal number (00-59) |
| %Y | Year with century as a decimal number (e.g., 2025) |
| %y | Year without century as a decimal number (00-99) |
| %c | Date and time representation appropriate for the locale |
| %x | Date representation appropriate for the locale |
| %X | Time representation appropriate for the locale |
| %p | AM/PM indicator for 12-hour clock |
| %Z | Time zone name |
| %% | A literal ‘%’ character |
Finally, in the loop section, we simply call printDateTime() every second to print the current date and time to the Serial Monitor.
void loop() { // Print formatted date and time printDateTime(); delay(1000); }Tag » Arduino Ntpclient Daylight Saving
-
ESP32 NTP Time - Setting Up Timezones And Daylight Saving Time
-
Network Time Protocol (NTP), Timezone And Daylight Saving Time ...
-
NTP, Daylight Saving Time Switching Problem. - Arduino Forum
-
ESP8266 - NTP Client And Daylight Saving Time - Arduino Slovakia
-
NTP For The ESP8266 Including Day Light Saving Time (DST) Without ...
-
Question: Is There A Way To Set The Time Offset Automatically #40
-
NTP Server Library With Timezones/daylight Savings Time : R/arduino
-
The Arduino And Daylight Saving Time (Europe) - Instructables
-
ESP32 NTP Client-Server: Get Date And Time (Arduino IDE)
-
Summer Winter Time Micropython Vs Arduino - RNT Lab
-
#299 Tricks To Get NTP Time For The ESP32 And The ESP8266 Incl ...
-
Get Date From Ntp Server