This question already has answers here:
How do I convert a datetime to date?
(10 answers)
Closed 3 years ago.
I have a datetime object
v = 21.01.2019 14:25:37
I want to convert above datetime object to date as this
a = convert(v)
a = 21.01.2019
how i can do it
If you want today's date.try this :
datetime.datetime.now().date()
If you want your datetime object date :
v.date()
Related
This question already has answers here:
Convert string into datetime.time object
(4 answers)
Closed 1 year ago.
I have a string in the str format
PATTERN_OUT = "%H:%M"
date_time = (datetime.strftime(enddateandtime, PATTERN_OUT))
I need to convert it to datetime.time. How can this be done?
You can utilize the fact that time string is \d\d:\d\d.
Look at the following snippet.
from datetime import time
time_str = "10:01"
time(*map(int, time_str.split(':')))
Add exception handler if required.
This question already has answers here:
Add 1 day to my date in Python [duplicate]
(1 answer)
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I'm trying to add one day to custom date(in string). Time format is dd/mm/yyyy
Sample input:
'02/11/2020'
Output:
'03/11/2020'
from datetime import datetime, timedelta
date = '02/11/2020'
old = datetime.strptime(date, "%d/%m/%Y")
new = old + timedelta(days=1)
new_date = new.strftime("%d/%m/%Y")
print(new_date) # '03/11/2020'
You can use the datetime module
from datetime import datetime, timedelta
start = datetime.strptime("%d/%m/%Y", "02/11/2020")
end = start + datetime.timedelta(days = 1)
This question already has answers here:
How to get day name from datetime
(6 answers)
Closed 3 years ago.
Is it possible to get the name of a day (e.g. Monday) from a datetime object?
In my pandas dataframe I have a column of type datetime e.g.
2016-07-01
and so on
I want to create a new column with the name of that day
Thanks in advance
duplicate of this?
How to get day name in datetime in python?
import datetime
now = datetime.datetime.now()
print(now.strftime("%A"))
or:
>>> from datetime import datetime as date
>>> date.today().strftime("%A")
'Monday'
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
I have a variable '2019-05-30 21:01:09' that needs to be converted into 2019-05-30 21:01:09. What is the best way to go about this.
from datetime import datetime
strToDate = datetime.strptime('2019-05-30 21:01:09','%Y-%m-%d %H:%M:%S')
strptime() allows for the conversion of string to date time object provided that you give the format the function should expect as the second argument
You can using datetime library
from datetime import datetime
datetime.strptime('2019-05-30 21:01:09', '%Y-%m-%d %H:%M:%S')
https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
This question already has answers here:
How can I parse a time string containing milliseconds in it with python?
(7 answers)
Closed 7 years ago.
How to convert "2013-10-21 12:00:00.004" to datetime object in Python?
The problem is there is decimal number in seconds.
here is a working example
import datetime
dt = datetime.datetime.strptime("2013-10-21 12:00:00.004", "%Y-%m-%d %H:%M:%S.%f")
print (dt.microsecond)