how to convert date in month python? [duplicate] - python-3.x

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 3 years ago.
i m currently working in Generating dynamic invoice on monthly basis but i m stuck in getting month name in python from user input date
Example: Given Date is :15/03/2020 is date and month is March
Any help would be appreciated

Assuming that you are receiving the date in the form of a string like "15/03/2020":
int(date.split('/')[1]) returns the value between the first and second / of date as an integer. This can then be used as the index for a list of month names to return the name of the month:
months = ["January","February","March",...]
print(months[int(date.split('/')[1])-1])

Related

Current week in Python? [duplicate]

This question already has answers here:
Python: give start and end of week data from a given date
(5 answers)
how to get all dates of week based on week number in python
(7 answers)
Adding days to a date in Python
(16 answers)
Closed 9 months ago.
good morning, I'm in the middle of a project and I'm stuck on finding a way to return every day of the current week in a list. It would be something like this:
week=[ '2022-06-07' , '2022-06-08' , '2022-06-09' , '' , '...']
I would be very grateful if you could explain to me a way to obtain this result,,, thanks
You can shorten this but I wrote it out like this for clarity.
from datetime import datetime, timedelta
date_obj = datetime(2022, 6, 7) # Today, Tuesday
monday = date_obj - timedelta(days=date_obj.weekday()) # assuming start of week is Monday
week = []
for i in range(7):
week.append((monday + timedelta(days=i)).strftime("%Y-%m-%d"))
print(week)
Output:
# Monday 6/6 to Sunday 6/12
['2022-06-06', '2022-06-07', '2022-06-08', '2022-06-09', '2022-06-10', '2022-06-11', '2022-06-12']

pandas time series offset in months using frequencies notation [duplicate]

This question already has an answer here:
How can I get pandas Timestamp offset by certain amount of months?
(1 answer)
Closed 2 years ago.
I'd like to offset a date in a DatetimeIndex by a certain number of months using a string but can only find reference to the MonthEnd version.
Here is my (incorrect) attempt
rng = pd.date_range('2020-01-01','2020-07-03')
rng[-1] - pd.tseries.frequencies.to_offset('2m')
This will return Timestamp('2020-05-31 00:00:00') when instead I'm looking for Timestamp('2020-05-03 00:00:00')
Is there an alternative to '2m' here which will give me that result?
The DateOffset method in pandas works well:
rng[-1] - pd.DateOffset(months=2)
Output:
Timestamp('2020-05-03 00:00:00')

How to calculate total number of days in between two dates in groovy [duplicate]

This question already has answers here:
Duration between two dates in Groovy
(2 answers)
Closed 9 years ago.
How to calculate total number of months and year in between two dates in groovy
date1 =2012 nov 1
date2 =2013 feb 1
required output is year = 0 and month =3.
Any answer is helpful
I think this should do:
monthBetween = (start[Calendar.MONTH] - end[Calendar.MONTH]) + 1
yearsBetween = start[Calendar.YEAR] - end[Calendar.YEAR]
months = monthBetween + (yearsBetween * 12)
Taken from Calculate difference in months between two dates with Groovy

Week number of a year in dateformat

I need to get the year I use the format YYYY, and month MM, and so on. I am looking to see if there is a format to get the week number of the year.
The date that I am using as example is 2008-03-09 16:05:07.000, and I would like to know the week number which is 11 in this case. Is there a way to retrieve 11 programmatically?
There's no format that provides the weeknumber, but you can get it in this way easily:
System.Globalization.CultureInfo cul = System.Globalization.CultureInfo.CurrentCulture;
int weekNum = cul.Calendar.GetWeekOfYear(
DateTime.Now,
System.Globalization.CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
CultureInfo.Calendar Property
Calendar.GetWeekOfYear Method
Custom Date and Time Format Strings
To answer your comment how to get the quarter of a year of a given DateTime:
int quarter = (int)((DateTime.Now.Month - 1) / 3) + 1

Sharepoint - End date minus Start date = Years?

I am needing to calculate the number of years between two fields;
Field A
Start Date (dd/mm/yyyy)
Field B
End Date (dd/mm/yyyy)
Field C
Calc formula display difference between Field A and B in years and months i.e 5.2
Thanks all.
Assuming you are asking about Calculated Columns then you need the DATEDIF function
Also I am assuming that the 5.2 is 5 years and 2 months - not 5 years and .2 of a year - be a bit more specific in your question and you will get better answers.
So
= DATEDIF([Start Date],[End Date],"Y")
& " years and " &
DATEDIF([Start Date],[End Date],"YM")
& " months "
Examples of common formulas

Resources