Python convert datetime string to date - python

I have this string (It's come from a variable. as a example, st_date)
'2015-01-28 03:00:00'
and I want to parse the date and convert to type:date
datetime.date 2015-01-28
from it.. How could I do that?

Use datetime.strptime() which takes two arguments, your date as a string and the format you want.
from datetime import datetime
my_date = datetime.strptime('2015-01-28 03:00:00', '%Y-%m-%d %H:%M:%S')
my_date.year
>> 2015

import datetime
import time
date_str_obj = '2015-01-28 03:00:00'
date_date_obj = datetime.datetime.strptime(date_str_obj, '%Y-%m-%d %I:%M:%f')

Just read the docs https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime
from datetime import datetime
a = '2015-01-28 03:00:00'
print datetime.strptime(a[:10], '%Y-%m-%d')

Use the strptime() function.
datetime.datetime.strptime('2015-01-28 03:00:00','%Y-%m-%d %H:%M:%S') #24-hour clock
datetime.datetime.strptime('2015-01-28 03:00:00','%Y-%m-%d %I:%M:%S') #12-hour clock

You can use easy_date to make it easy:
import date_converter
my_date = date_converter.string_to_date('2015-01-28 03:00:00', '%Y-%m-%d %H:%M:%S')

from datetime import datetime
date = datetime.strptime('2015-01-28 03:00:00','%Y-%m-%d %H:%M:%S')
date.date()

Use dateutil:
import dateutil.parser
dateutil.parser.parse('2015-01-28 03:00:00').date()
>>datetime.date(2015, 1, 28)

Related

Get only date from datetime generated string

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

How to fix date formatting using python3

I have data with the date format as follows:
date_format = 190410
year = 19
month = 04
date = 10
I want to change the date format, to be like this:
date_format = 10-04-2019
How do I solve this problem?
>>> import datetime
>>> date = 190410
>>> datetime.datetime.strptime(str(date), "%y%m%d").strftime("%d-%m-%Y")
'10-04-2019'
datetime.strptime() takes a data string and a format, and turns that into datetime object, and datetime objects have a method called strftime that turns datetime objects to string with given format. You can look what %y %m %d %Y are from here.
This is what you want(Notice that you have to change your format)
import datetime
date_format = '2019-04-10'
date_time_obj = datetime.datetime.strptime(date_format, '%Y-%m-%d')
print(date_time_obj)
Here is an other example
import datetime
date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')
print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
You can also do this
from datetime import datetime, timedelta
s = "20120213"
# you could also import date instead of datetime and use that.
date = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
print(date)
There are many ways to achieve what you want.

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 I do convert from timestamp to date in python?

I have this string '2015-04-08T07:52:00Z' and I wanna to convert it to '08/04/2015', how can I do this?
You can use the datetime.datetime.strptime() function to create a datetime object, then datetime.datetime.strftime() to return your correctly formatted date like so:
from datetime import datetime
dt = datetime.strptime('2015-04-08T07:52:00Z', '%Y-%m-%dT%H:%M:%SZ')
print dt.strftime('%d/%m/%Y')
You can use easy_date to make it easy:
import date_converter
converted_date = date_converter.string_to_string('2015-04-08T07:52:00Z', '%Y-%m-%dT%H:%M:%SZ', '%d/%m/%Y')

Categories

Resources