Python convert integer to date [duplicate] - python

This question already has answers here:
How to convert a given ordinal number (from Excel) to a date
(5 answers)
Closed 7 years ago.
I have this list of integers and need to convert it to date using python.
42222 should be 8/6/2015
42290 should be 10/13/2015
42319 should be 11/11/2015
I get the equal date of the integer when i paste in to excel then format the cell to Date.

Excel dates start counting around the year 1900. This will do it:
from datetime import datetime, timedelta
def xldate_to_datetime(xldate):
tempDate = datetime(1900, 1, 1)
deltaDays =timedelta(days=int(xldate)-2)
TheTime = (tempDate + deltaDays )
return TheTime.strftime("%m/%d/%Y")
>>> xldate_to_datetime(42290)
'10/13/2015'

Related

Convert a number sequence into a date using python? (example: 221201) [duplicate]

This question already has answers here:
How can I convert a string into a date object and get year, month and day separately?
(4 answers)
Closed 2 months ago.
I have a bunch of number sequences like "221201" meaning the year 2022, month 12 and day 01 (so 2022-12-01). How can I convert number sequences in this format into the actual date using python?
I've tried using the dateutil library but couldn't figure out how to get it to recognize this format.
from datetime import datetime
date_str = '221201'
date_obj = datetime.strptime(date_str, '%y%m%d')
print(type(date_obj))
print(date_obj) # printed in default format
You can learn more about strptime in this link

How to add - to the cell that has dates [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
How can I format date time string which is not date time format so that I can use it with pd.to_datetime()?
(2 answers)
Convert DataFrame column type from string to datetime
(6 answers)
Closed 5 months ago.
I have a 'date' column that has a date value as 20170423 (yyyymmdd) how can i change it to 2017-04-23?
dataframe = df
column name 'date'
I read the below post, but none of the solutions worked
Fastest way to insert these dashes in python string?
Example
d = df['Date']
df['Date'] = '%s-%s-%s' % (d[:4], d[4:6], d[6:])
The output 0 0 20170711\n1 20170718\n2 20170718\n3... when i exported in .csv

Change the date format to its respective month and year only in python [duplicate]

This question already has answers here:
How to convert a date string to different format [duplicate]
(2 answers)
Closed 1 year ago.
I have a date say - 25-Jan-19
I want to convert it to - Jan19 in python. What date format I'll have to use to get this?
If 25-Jan-19 is a string and will always be in this format, you can try this:-
date = date.split("-")
date = "".join(date[i] for i in range(1,len(date)))

Python: Add one day to custom date? [duplicate]

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)

How to format datetime object to date [duplicate]

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()

Categories