ESP/Arduino Date and Time
The Arduino cores for the ESP8266 and ESP32 natively support the current date and time, synchronised to NTP servers. There is no need for a real-time clock chip or for extra libraries. Just #include <time.h>, configure the timezone and NTP servers in setup() using configTime(), and you can use standard C functions like time(), localtime() and gmtime() in your code.
The function configTime(char * timezone, char * ntppool) takes a standard timezone string similar to those shown in the list here, and an NTP pool address. So for example in the UK, I would use:
configTime("GMT0BST,M3.5.0/1,M10.5.0", "uk.pool.ntp.org")
which specifies BST between the last Sunday in March at 1am and the last Sunday in October at 2am.
For systems that have to support different timezones, the timezone could also be geolocated using an online API, for example ipgeolocation.io.
The current date and time will obviously not be valid on startup, until the ESP has synchronised with an NTP server. A callback function can be configured in setup() by using settimeofday_cb(function * callback). This function will be called every time the date and time is set by NTP, and can be used to set a flag to indicate that the time and date is now valid.