How to convert my date format to another format - python

My Date for mat is below
import time
timestr = time.strftime("%Y%m%d-%H%M%S")
My out >> 20200607-080127
I need timestr above to convert to 07/06/2020 08:01:27
I need to use timestr only to convert

Please, use the following:
>>> import datetime
>>> timestr = datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")
>>> timestr
'07/06/2020 17:08:31'
datetime is recommended over time because there are a few formatting strings that are not supported by time, but work just find with datetime.

You can specify every format you want, so for your desired output use:
time.strftime("%d/%m/%Y %H:%M:%S")
You can do all sort of stuff like:
time.strftime("%d//%m//%Y %H~%M~%S")
Hope that helped

Please add the required separators.
import time
timestr = time.strftime("%d/%m/%Y %H:%M:%S")
print(timestr)

from datetime import datetime
datetime_str = '20200607-080127'
datetime_object = datetime.strptime(datetime_str, '%Y%m%d-%H%M%S')
print(type(datetime_object))

Related

How to format datetime to format "2021-04-30T10:30:00+12:00" in Python?

The below code returns as format 2021-04-30 10:30:00+12:00
time_now = datetime.now(timezone(settings.TIME_ZONE)).replace(microsecond=0)
How to get it in format 2021-04-30T10:30:00+12:00 please?
You can use astimezone(). For example:
from datetime import datetime
time_now = datetime.now().astimezone()
my_format = time_now.strftime("%Y-%m-%dT%H:%M:%S%z")
%z is empty string if object is naive. That's why you can't print it.

Extract only time (HH:MM) from datetime field - Odoo 10

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'

Modify datetime.now() standard output

I what to change standard time output to "%Y-%m-%dT%H:%M:%S.%f+00:00" format, how can I do it?
Here is what i have now.
import datetime
start_date = datetime.datetime.now()
print(start_date,"+00:00")
#output: 2019-04-19 06:57:47.563791 +00:00
#what to be: 2019-04-19T06:57:47.563791+00:00
Use exactly that format string with datetime.strftime:
datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f+00:00")
Use datetime.strftime to convert your datetime object to the required format
import datetime
start_date = datetime.datetime.now()
print(start_date.strftime("%Y-%m-%dT%H:%M:%S.%f+00:00"))
#2019-04-26T12:50:45.219062+00:00

Python convert date to datetime

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

How to convert a time to a string

I am using the date time lib to get the current date. After obtaining the current date I need to convert the obtained date to in to a string format. Any help is appriciated
from datetime import date
today=date.today()
print today
You can use today.strftime(format). format will be a string as described here http://docs.python.org/library/time.html#time.strftime . Example:
from datetime import date
today = date.today()
today.strftime("%x")
#>>> '01/31/11'
datetime.strftime allows you to format a datetime however you want
stringDate = str(today)
If that's what you want.
In Python3, you can use f-string formatting:
from datetime import date
today=date.today()
today_str = f"{today:%m/%d/%y}"
today_str
#>>>'06/24/22'

Categories

Resources