im trying to analyze the amount of titles and the time in a subreddit with Python. But i cant convert the UTC time to datetime
here is my code
hotPython = subreddit.hot(limit=4)
for i in hotPython:
print(i.created_utc)
and i get back this results
1523290473.0
1521831644.0
1523750525.0
1523747490.0
i tried different thing to convert, but i dont get it
would appreciate if somebody can help
If you're able to get the epoch time, you can convert this to a datetime using datetime.fromtimestamp.
For example:
from datetime import datetime
t = 1523290473.0
dt = datetime.fromtimestamp(t)
Then dt is a datetime as desired.
Related
I need to get time in python and i am using time.ctime() and it returns this Thu Jul 8 15:37:26 2021
But i only need 15:37:26 and i cant figure out how to get only this and not the date and year.
I already tried using datetime where i could not figure it out either so im trying with time now.
here is a bit of code for the context:
cas = time.ctime()
cas = str(cas)
api.update_status('nyni je:'+ cas )
time.sleep(60)
Anyone know how to do it?
print(datetime.datetime.now().time().isoformat(timespec='seconds'))
import datetime
print(datetime.datetime.now().strftime("%H:%M:%S"))
imports
from datetime import datetime
code
now = datetime.now()
cas = now.strftime("%H:%M:%S")
print(cas)
You can use strftime to convert a datetime value to a string of a certain format. In your case you can use %H:%M:%S to only get the time. The same function can be used to get the date as well, you can read more here.
Take a look at the "strftime() and strptime() Format Codes" section also for how you can format it.
I have a question based on the conversion of a date and time using the start_time element's Google Calendar API, how could I convert it to a day, month and year of Latin America without more, if you could help me please
the datetime:
2021-01-19T09:00:00-05:00
I need the follow example: day/mount/year 15/01/2021
I've been trying and can't find a way to convert properly, please if you could help me.
Use the datetime library:
import datetime
date_str = "2021-01-19T09:00:00-05:00"
date_obj = datetime.datetime.strptime(date_str[:10], "%Y-%m-%d").strftime("%d/%m/%Y")
print(date_obj)
I am trying to use timestamp information to graph.
However, when I convert the number into hh:mm:ss. It does not work.
I have tried this:
timestamp = [dt.strptime(str(datetime.timedelta(seconds=round(t/1000))),'%H:%M:%S') for t in timestamp1]
Also I tried this
timestamp = [dt.strftime(dt.strptime(str(datetime.timedelta(seconds=round(t / 1000))), '%H:%M:%S'), '%H:%M:%S') for t in timestamp]
However, it is possible to see the list with the new values. However, I have problems with the graphs and these new values.
Can anybody help me?
Try using this and make sure to import the time module!
timestamp = [time.strftime('%H:%M:%S', time.gmtime(t/1000)) for t in timestamp1]
In your code you're using datetime.strptime(date_string, format) function, it's main use is to parse time strings in order to create datetime object.
In order to get a parsed string of a desired format you should use date.strftime(format) function instead.
So basically in your code you can only add it and should get the wanted result:
import datetime
from datetime import datetime as dt
timestamp = [dt.strftime(dt.strptime(str(datetime.timedelta(seconds=round(t / 1000))), '%H:%M:%S'), '%H:%M:%S') for t in timestamp1]
I'm not sure what was your input and desired output, but you could also use date.fromtimestamp(timestamp) for the parsing of timestamps
Is there a way to convert UTC time to a formatted string containing the date?
I currently have a list full UTC time but I need them to be formatted as %Y-%m-%d%H:%M:%S.%f but as a string.
you can use the datetime library. I have used this in the past and it works
from datetime import datetime, timezone
datetime.now(timezone.utc).strftime("%Y%m%d")
This should work:
import datetime
datetime.datetime.strptime("1998-04-18 16:48:36.0","%Y-%m-%d %H:%M:%S.%f").strftime("%Y-%m-%d%H:%M:%S.%f")
'1998-04-1816:48:36.000000'
I have a field that shows time as 1900-01-01 00:05:00 but I want to show it as just 00:05:00 i.e. just five minutes.
I have been looking at the python documentation and it is driving me crazy. From the question How do I find the time difference between two datetime objects in python? it looks like is should be something do with timedelta but I am not getting any closer to a solution. The question Converting Date/Time to Just Time suggests you can just use a format tag but that is not working for me. I also looked at converting date time to string without success, I know this is something simple but I have looked at hundreds of pages looking for something simple. All help would be appreciated.
Say you have a Datetime object now:
now.time().strftime('%H:%M:%S')
Load the string with strptime() and get the time() component:
>>> from datetime import datetime
>>> s = "1900-01-01 00:05:00"
>>> dt = datetime.strptime(s, "%Y-%m-%d %H:%M:%S")
>>> dt.time().isoformat()
'00:05:00'
Here we are dumping the time with isoformat() in ISO 8601 format, but you can also dump the datetime.time back to string with strftime():
>>> dt.time().strftime("%H:%M:%S")
'00:05:00'