Originally I made this code to convert date into human readable time:
a = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S.%f") b = datetime.datetime.now() c = b - a days, hours, minutes, seconds = int(c.days), int(c.seconds // 3600), int(c.seconds % 3600 / 60.0), int(c.seconds % 60.0) return days, hours, minutes, seconds EXAMPLE OUTPUT: 1 days, 4 hours, 24 minutes, 37 seconds
and I'm trying to make it using epoch time, but I have no idea on to make it calculate days hours and etc.
a = last_epoch #last epoch recorded b = time.time() #current epoch time c = b - a #returns seconds hours = c // 3600 / 24 #the only thing I managed to figure out
Convert from epoch to human-readable dateString date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000)); Epoch in seconds, remove '*1000' for milliseconds. myString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch.
Use the timedelta() constructor and pass the seconds value to it using the seconds argument. The timedelta constructor creates the timedelta object, representing time in days, hours, minutes, and seconds ( days, hh:mm:ss.ms ) format. For example, datetime.
To convert a datetime to seconds, subtracts the input datetime from the epoch time. For Python, the epoch time starts at 00:00:00 UTC on 1 January 1970. Subtraction gives you the timedelta object. Use the total_seconds() method of a timedelta object to get the number of seconds since the epoch.
import datetime timestamp = 1339521878.04 value = datetime.datetime.fromtimestamp(timestamp) print(value.strftime('%Y-%m-%d %H:%M:%S'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With