This question already has answers here:
How can I get the previous week in Python?
(1 answer)
How to get Year-Week format in ISO calendar format?
(2 answers)
Closed last month.
I have a year and week number in an integer format, and I would like to get the previous week number.
I currently have this code:
# get previous week number (as an integer)
weeknumber = 202302
year = weeknumber//100
week = weeknumber-year*100
day = 1
prev_weeknumber = int(''.join(map(str,((datetime.fromisocalendar(year,week,day))- timedelta(days = 7)).isocalendar()[0:2])))
But it provides the following output
prev_weeknumber = 20231
While I need it to have this output
prev_weeknumber = 202301
Related
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
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)))
This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 2 years ago.
I have in Python 3.7 a date in form of an integer that represents the number of hours from 1/1/1900 00 Hours. Can I transform it into string of format dd/mm/yyyy?
for example:
timenumber = 1043148
timestring = magictrickfunctions(timenumber)
print(timestring)
should give "01/01/2017"
Are you sure it gives you 01/01/2017 instead of 01/01/2019?
To obtain the number of days divide your total hours by /24 this will give you the number of days. You can then specify that as shown below. I am using pandas library here.
import pandas as pd
start_date = "01/01/1900"
date_1 = pd.to_datetime(start_date)
end_date = date_1 + pd.DateOffset(days=43464) #specify the number of days and add it to your start_date
print(end_date)
Outtput
2019-01-01 00:00:00
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'
This question already has answers here:
Python: Number of the Week in a Month
(9 answers)
Closed 7 years ago.
I want to get week number like picture below
If I insert 20150502, It should print "week 1".
If I insert 20150504, It should print "week 2".
If I insert 20150522, It should print "week 4".
How to get week number?
Here's a quick attempt, as with all date/time code there are probably a lot of edge cases that can cause strange results.
# dateutil's parser is very good for converting from string to date
from dateutil.parser import parse
date_strs = ['20150502', '20150504', '20150522']
for date_str in date_strs:
d = parse(date_str)
month_start = datetime.datetime(d.year, d.month, 1)
week = d.isocalendar()[1] - month_start.isocalendar()[1] + 1
print(date_str + ':', week)
Output:
20150502: 1
20150504: 2
20150522: 4