Getting a "timestamp" of when data is collected is entirely down to you.
Most Arduinos don't have any concept of the current time, only the time since the program started running. To know what the time "now" is you have to have some mechanism to tell the Arduino what the time is, along with a method of keeping track of that time.
There are devices called Real-Time Clock (RTC) modules which keep track of the time for you. They don't magically know the time - you still have to tell them at least once.
You could tell it the time through the serial port to set the clock - from then on (assuming the RTC has power) the RTC will know what the time is.
Another option to get the time into the RTC is to use an internet connection (ESP8266, WiFi shield, Ethernet shield, etc) to perform a Network Time Protocol (NTP) query to a time server on the internet (such as pool.ntp.org) to get the current time and update the RTC. This should be done regularly to correct any drift in the RTC.
Once you have an RTC and a method of setting the time you can query the time whenever you sample some data and store that time along with the data in whatever way is most suitable for your situation.
Answer from Majenko on Stack ExchangeGetting a "timestamp" of when data is collected is entirely down to you.
Most Arduinos don't have any concept of the current time, only the time since the program started running. To know what the time "now" is you have to have some mechanism to tell the Arduino what the time is, along with a method of keeping track of that time.
There are devices called Real-Time Clock (RTC) modules which keep track of the time for you. They don't magically know the time - you still have to tell them at least once.
You could tell it the time through the serial port to set the clock - from then on (assuming the RTC has power) the RTC will know what the time is.
Another option to get the time into the RTC is to use an internet connection (ESP8266, WiFi shield, Ethernet shield, etc) to perform a Network Time Protocol (NTP) query to a time server on the internet (such as pool.ntp.org) to get the current time and update the RTC. This should be done regularly to correct any drift in the RTC.
Once you have an RTC and a method of setting the time you can query the time whenever you sample some data and store that time along with the data in whatever way is most suitable for your situation.
It depends on how you define the time stamp. A Unix timestamp is the number of seconds elapsed since Unix epoch time, i.e. January 1 1970 00:00 UTC, this is a very common time stamp. You can setup with NTP via the internet, or you can use a RTC on your board. How you do it depends on how accurate you want it and what you have available. Try this link it may help: https://currentmillis.com/
NTP is the proven way of getting time remotely. NTP libraries, like @Marcel denotes, is making UDP connections to a server with an interval. So you do not need to do any polling to the server before using it.
Here is the usage of an NTP library with a one-hour offset and one minute refresh interval:
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define NTP_OFFSET 60 * 60 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS "europe.pool.ntp.org"
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
void setup(){
timeClient.begin();
}
void loop() {
timeClient.update();
}
To get a timestamp or formatted time anytime you want, use the functions:
String formattedTime = timeClient.getFormattedTime();
unsigned long epcohTime = timeClient.getEpochTime();
Looking at that code I don't think this microcontroller directly has a clock built in as mentioned. Since you have Wi-Fi though you could make a web query to get it instead.
I'd use a REST query to a place like this:
https://timezonedb.com/api
Which will give you back a JSON formatted time. If you only need accuracy +/- a few seconds that will be fine. You could lower the bandwidth / improve battery life by setting the time like this and then using an internal timer to calculate an offset instead of making a query every time you need a time stamp. Eventually you would need to requery the time and 'correct' it since your timer on that probably is not accurate for long periods of time, plus it could eventually roll over.
If you need the time more accurately then that you will likely need a clock. You could try to do a bit of correction based on ping, but all in all how accurate it needs to be is based on your project requirements.
Videos
You can use this python script as your receiver code. Change 'port' to your Arduino port.
To find your Arduino port, before you plug it in, in the shell terminal use
ls /dev/tty*
Use it again after you have plugged your Arduino in and compare the lists to find the new connection. (For me, it ended up being ACM0)
Do not forget to download serial library if it is missing: https://pypi.python.org/pypi/pyserial
import serial
import time
import datetime
ser = serial.Serial(
port='COM5',\
baudrate=9600,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)
print("connected to: " + ser.portstr)
while True:
line = ser.readline()
timestamp = str(time.time())
#timestamp = str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))
with open('output.txt', 'a') as pyfile:
pyfile.write(line + ' ' + timestamp +'\n')
ser.close()
In case you are on Windows, you can Try this Free console application: https://hiterminallogger.sourceforge.io/ Just download and run the exe in a bash console emulator
$./HiTerminalLogger.exe COM<portnumber> 9600
you will get it Timestamped log as well as html report. you can as well as highlight the Text to make it more conspicuous.

An accurate enough way is to use the millis() function. It will return the value in milliseconds since the start of the Arduino. If you start the Arduino at a specific time, you will be able to calculate the exact date and time.
Why not an external module?? An RTC such as the DS3231 in merely 5$ and it includes a temperature sensor for you!
without external source
You can't. The Arduino Uno has no real-time clock. A real-time clock is only something like $1 from eBay. For example the DS1307 or DS3231. I found 5 x boards pre-assembled with the clock chip, including battery holder, crystal, chip, and circuit board for $US 4.20 on eBay. Batteries not supplied.
Hook that up to the I2C pins (A4 and A5), set the time once using a suitable sketch, and then you are ready to roll.
If you just want to do something every 24 hours (not necessarily at 9:36 a.m.) then you can just use millis to find when the appropriate number of milliseconds has elapsed. The result from millis will wrap every 49 days roughly but you don't have to worry about that.
Note that this won't let you log the date and time, but you can log something (eg. the temperature) every day, you would just have to know when you started logging.