I'm trying to get the current timestamp and then its hour by doing the following:
from datetime import datetime
curr = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
end_date = curr.split(':')[0]+':00:00'
I'm getting the output for end_date as follows: 2018-11-10 10:00:00. But this is currently in UTC time. How do I get it in PDT time?? Thanks!!
You can do this by importing the pytz module. Also, you can just generate the date format you need from the datetime object. So based on your code snippet, PDT time can be obtained like this.
from datetime import datetime
import pytz
curr = datetime.now(pytz.timezone('US/Pacific'))
full_date = curr.strftime('%Y-%m-%d %H:%M:%S')
hour_date = curr.strftime('%Y-%m-%d %H:00:00')
You will have to import pyzt module to get the time for the time zone that you want. Check this documentation
Related
I have date in the as string in the following format: 202001010000
I am trying to convert this to UNIX format and get the result in UTC
I did:
import datetime
stime = "202001010000"
print(int(datetime.datetime.strptime(stime, "%Y%m%d%H%M").replace(tzinfo=datetime.timezone.utc).timestamp()))
and this is giving me the output in UNIX, but in CEST format.
With the above code I get: 1577836800 but I want the output to be 1577833200
What is the mistake I am doing?
You're setting time zone to UTC when converting to datetime. But since your input represents time in Germany you want a time zone that is active there. EX:
from datetime import datetime
from zoneinfo import ZoneInfo # Python 3.9+, can use backports.zoneinfo for older versions
stime = "202001010000"
# stime represents time in Germany so we use time zone
time_zone = ZoneInfo('Europe/Berlin')
# to datetime, with tz set:
dtobj = datetime.strptime(stime, "%Y%m%d%H%M").replace(tzinfo=time_zone)
# unix time
ts = dtobj.timestamp()
print(ts)
# 1577833200.0
# back to datetime, again specify time zone
dtobj = datetime.fromtimestamp(ts, tz=time_zone)
print(dtobj)
# 2020-01-01 00:00:00+01:00
Note that if the input represents the same time zone your OS is configured to use, this works correctly without setting a time zone. But I think it's better to be explicit here, to avoid confusion if you e.g. run this script on a machine configured to use another time zone.
What you're trying to get is 7 hours behind and you cannot do that from your start date. You must push your start date back 1 day and push your hours forward 17. This code will work for you
import datetime
stime = "201912310000"
my_date = datetime.datetime.strptime(stime, "%Y%m%d%H%M")
my_date_utc = my_date.replace(hour=17)
my_timestamp = my_date_utc.timestamp()
print(int(my_timestamp))
I have a timestamp that was created with datetime module and now I need to convert '2020-10-08T14:52:49.387077+00:00' into 08/OCT/2020?
Can datetime do this? I have tried strptime but I think the +00:00 at the end is causing errors.
Use fromisoformat and strftime method from datetime package in Python 3.7:
from datetime import datetime
time = '2020-10-08T14:52:49.387077+00:00'
new_time = datetime.fromisoformat(time).strftime("%d/%b/%Y")
print(new_time)
Or with strptime:
from datetime import datetime
time = '2020-10-08T14:52:49.387077+00:00'
new_time = datetime.strptime(time.split('T')[0], "%Y-%m-%d").strftime("%d/%b/%Y")
print(new_time)
Output:
08/Oct/2020
I am trying to get only the year output from the below script, but when I check for the same I am getting
/year=2020-08-20 19:11:26.616679/month=2020-08-20 19:11:26.616689/date=2020-08-20 19:11:26.616690'
Is there a way to get in year=2020, month=08, and date=20 format?
year = datetime.datetime.now()
from datetime import date
print(date.today().strftime('%Y, %m, %d'))
With datetime module, you can have something like this
from datetime import datetime
year = datetime.now()
print(datetime.strftime(year, 'year=%Y, month=%m, date=%d'))
I'a m trying to extract only time (Hour:Minute) from datetime field
Example:
today_with_hour = fields.Datetime(
string=u'hora',
default=fields.Datetime.now,
)
I would like to know how get only hour from today_with_hour in format
17:10:20
This is one way to extract:
from datetime import datetime
now = datetime.now()
print(str(now.hour)+':'+str(now.minute)+':'+str(now.second))
This may be better way to do it
You can use strftime
Example:
from datetime import datetime
datetime.now().strftime("%H:%M:%S")
In your case you can follow like this:
from datetime import datetime
datetime.strptime('20/06/2019 17:28:52', "%d/%m/%Y %H:%M:%S").time()
Output will be:
17:28:52
Better way to do is by using strftime().
dt = datetime.strptime('20/06/2019 17:28:52', "%d/%m/%Y %H:%M:%S")
dt.strftime("%H:%M:%S")
output:
17:28:52
To get the current time from the datetime.now()
datetime.datetime.now().time().strftime("%H:%M:%S")
O/P:
'11:16:17'
if you want to get the time with milliseconds also, use isoformat()
datetime.datetime.now().time().isoformat()
O/P:
'11:20:34.978272'
I have following string of date "2018-05-08" and I would like to convert it into the datetime format of python like: "2018-05-08T00:00:00.0000000". I understand I can combine the string like:
> date = "2018-05-08"
> time = "T00:00:00.0000000"
> date+time
'2018-05-08T00:00:00.0000000'
But is there pythonic way of doing it where I can use libraries like datetime?
You can use datetime module like this example:
import datetime
date = "2018-05-08"
final = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%Y-%m-%dT%H:%M:%S.%f')
print(final)
Output:
2018-05-08T00:00:00.000000
For more details visit datetime's documentation
Use this code
from datetime import date
from datetime import datetime
d = date.today()
datetime.combine(d, datetime.min.time())
from datetime import datetime
datetime.strftime(datetime.strptime('2018-05-08','%Y-%m-%d'),'%Y-%m-%dT%H:%M:%S.%f')
OUTPUT:
'2018-05-08T00:00:00.000000'
from datetime import datetime
date_as_datetime = datetime.strptime("2018-05-08", '%Y-%m-%d')
date_as_string = date_as_datetime.strftime('%Y-%m-%dT%H:%M:%S')
print(date_as_string)
Out:
'2018-05-08T00:00:00'
See strptime/strftime options here: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
Yes, you can use the datetime module and the strftime method from that module. Here is an example below if we want to format the current time.
import datetime
current_time = datetime.datetime.now()
formatted_time = datetime.datetime.strftime(current_time, "%Y-%h-%d %H:%m:%S")
print(formatted_time)
Here is the output:
2018-May-08 13:05:16