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
Related
I am trying to convert the local time into UTC time. But getting the below error.
Error: an integer is required (got type str)
from datetime import datetime
starts_date = '2021-07-30 09:30:00'(timestamp without time zone)
ts = starts_date
x = datetime.utcfromtimestamp(ts)
x_ts = x.timestamp()
Be sure that datetime is imported correctly as from datetime import datetime. Can be a bit confusing but the method utcfromtimestamp belongs to datetime.datetime and not datetime itself.
Here is a working example to convert a timestamp of (now) to a UTC timestamp.
from datetime import datetime as dt
# Create a timestamp object for now.
ts = dt.timestamp(dt.now())
# Convert now to a UTC timestamp.
dt.utcfromtimestamp(ts).timestamp()
>>> 1627637013.657752
datetime.utcfromtimestamp() takes an integer that represent the amount of seconds passed since January 1st 1970.
This means with
from datetime import datetime as dt
print(dt.utcfromtimestamp(0))
you get
1970-01-01 00:00:00
I importing this python modules
from datetime import date, timedelta, datetime
to_date = date.today()
from_date = to_date - timedelta(days=2)
print(f"Local Date and Time {datetime.datetime.now()}")
when I run this code I'm getting error like this_
type object 'datetime.datetime' has no attribute 'datetime'
Can anyone please tell how can I solve this problem.
The import line from datetime import date, timedelta, datetime means you imported
datetime.datetime access with datetime,
datetime.date access with date
datetime.timedelta with timedelta
So as you did for date and timedelta, now() is a method of datetime class and not datetime package :
to_date = date.today()
from_date = to_date - timedelta(days=2)
print(f"Local Date and Time {datetime.now()}") # not datetime.datetime.now()
See the basic example:
import datetime
x = datetime.datetime.now()
print(x)
I think you have put datetime 2 times wrongly. Since you are already importing datetime, it should be like this:
print(f"Local Date and Time {datetime.now()}")
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
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
I am trying to convert a datestamp of now into Unix TimeStamp, however the code below seems to be hit but then just jumps to the end of my app, as in seems to not like the time.mktime part.
from datetime import datetime
import time
now = datetime.now()
toDayDate = now.replace(hour=0, minute=0, second=0, microsecond=0)
newDate = time.mktime(datetime.strptime(toDayDate, "%Y-%m-%d %H:%M:%S").timetuple())
print(newDate)
Change
newDate = time.mktime(datetime.strptime(toDayDate, "%Y-%m-%d %H:%M:%S").timetuple())
to
newDate = time.mktime(datetime.timetuple())
as an example I did:
from datetime import datetime
from time import mktime
t = datetime.now()
unix_secs = mktime(t.timetuple())
and got unix_secs = 1488214742.0
Credit to #tarashypka- use t.utctimetuple() if you want the result in UTC (e.g. if your datetime object is aware of timezones)
You could use datetime.timestamp() in Python 3 to get the POSIX timestamp instead of using now().
The value returned is of type float. timestamp() relies on datetime which in turn relies on mktime(). However, datetime.timestamp() supports more platforms and has a wider range of values.