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)
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 am downloading a json file containing timestamp using python . But the timestamp i am getting is below format
`2021-04-01T21:43:52.757Z`
Want to convert into normal timestamp (CST Hrs). I also see that the time is increased by 4 hours when i compare the report manually.
`4/1/2021 5:43:53 PM`
The above hours is 4 hrs less when i compare with json file entry. Please advise me.
You need to use python's datetime module to handle this. The Z in the string actually means time zone 0 or UTC time which is 6 hours ahead of CST not 4:
import datetime
date_object = datetime.datetime.strptime(
"2021-04-01T21:43:52.757Z", "%Y-%m-%dT%H:%M:%S.%fZ"
)
date_object = date_object - datetime.timedelta(hours=6)
print(
f"{date_object.month}/{date_object.day}/{date_object.year} {date_object.strftime('%I:%M:%S %p')}"
)
Which will give this output:
4/1/2021 03:43:52 PM
have to use an f string if you want non zero padded dates because its not available in datetime according to the docs
You can use pytz module to deal with time zones directly and not hardcode them if you want. Or if you are on python 3.9 you can use timezone objects to create timezones yourself
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.
I'm really new to programming and I only just started using Python, if anyone could edit the code I put up to make it work how I want it to then please do.
I was wondering if I could make the date show up, on my python program but make it different for different regions, so if someone opened the program in United Kingdom the day would show up first and if it was in the US it would show the month or year first etc.
This is what I have so far.
import datetime
currentDate = datetime.date.today()
print(currentDate.strftime('Todays date is: %d %B %Y'))
I currently have it set so it shows the day first then the month then the year.
Is there anyway to make it use it in a different order depending on what country you're in?
Does this work for you?
>>> import datetime
>>> today = datetime.date.today()
>>> print(today.strftime('%x'))
09/10/15
Specifically, you probably should look at the %c, %x, and %X format codes. See 8.1.8. strftime() and strptime() Behavior for more information on how to use the strftime method.
I am somewhat new to Python and have a seemingly simple question.
I have a python script that interacts with an API (RHN Satellite if you're curious). This API returns a date in the form of a string and it always trims leading 0's. For example, 6/1/13 or 10/9/12. I need to convert this string to a date and determine the day of the year it is.
Here is what I know:
today = datetime.datetime.now()
print today.strftime('%j')
...will return today's day of year (175). This works fine for a datetime object but I am having trouble converting the string given by the API to an actual date. If I use:
date = datetime.datetime.strptime(var, '%m/%d/$y')
I get error:
ValueError: time data '5/2/13' does not match format '%m/%d/$y'
I'm guessing because it's expecting leading 0's ? How do I get around this?
In the end, I am trying to subtract the variable date given from the current date but I can't do that until I convert the string.
Thanks for the help!
I think you just have a typo, use %y instead of $y:
date = datetime.datetime.strptime(var, '%m/%d/%y')
This code works for me, provided you change $y to %y in the format code.
Correct the $y to %y and I'd use format instead of strftime:
from datetime import datetime
print format(datetime.strptime('5/2/13', '%m/%d/%y'), '%j')