I’m trying to make it so that it checks if its in the format I want and if its greater than today’s date. I want it to ask for input until it gets a right answer but I can’t find a way to do so. Answer You can use the today class method to get a datetime object representing today’s
Tag: python-datetime
Using datetime.strptime when data is a little messy : extra spaces, Jan or January
Currently the text I am dealing with are dates with a somewhat standard format, however the data isn’t super clean. For example the text can be in these formats: I’m not quite sure how to deal with this. I want to convert these strings into 2021-01-01 format. My plan was to convert to datetime obj…
Python 3.9: unpickling of IsoCalendarDate data returns a tuple
Consider the following discussion / feature in Python3.9: https://bugs.python.org/issue24416 In short, it was decided that the result of datetime.date.isocalendar would be changed to a namedtuple instead of tuple. Now, I can see the benefit in doing that, but they also decided “to pickle” the new …
How to get the EOMONTH(End of month) of the current date using Python3
I have a SQL server stored procedure and I am trying to replicate it using python. One of the things I am trying to replicate is the following function: Here is what I have tried in Python3: Output: The thing is, I have to enter the date (200,3,1). I want to be able to pick up the current date and
Django model DateTimeField set auto_now_add format or modify the serializer
I have this field in my model: And it is saved with this format: 2016-05-18T15:37:36.993048Z So I would like to convert it to this format DATE_INPUT_FORMATS = (‘%d-%m-%Y %H:%M:S’) but I dont know where to do it. I have a simple serializer class, could I override it to modify the format? or maybe c…
How to find out week no of the month in python?
I have seen many ways to determine week of the year. Like by giving instruction datetime.date(2016, 2, 14).isocalendar()[1] I get 6 as output. Which means 14th feb 2016 falls under 6th Week of the year. But I couldn’t find any way by which I could find week of the Month. Means IF I give input as some_fu…
timedelta error with numpy.longdouble dtype
I have times with dtype numpy.longdouble and when I’m trying to use that values with timedelta function I’ve got errors. But when I convert it to numpy.float64 everything is fine. Could somebody explain that behaviour? When I’m trying to see dtypes of variables they are look the similar but …
Calculate Time Difference Between Two Pandas Columns in Hours and Minutes
I have two columns, fromdate and todate, in a dataframe. I add a new column, diff, to find the difference between the two dates using I get the diff column, but it contains days, when there’s more than 24 hours. How do I convert my results to only hours and minutes (i.e. days are converted to hours)? An…
How do I determine if current time is within a specified range using Python’s datetime module?
What would be the best way to see if the current time lies between say 10:30 AM and 4:30 PM. I could think of the following, not sure how correct: Please let me know if this the correct approach, can something better be written? Answer My original answer focused very specifically on the question as posed and …
Python: how to compute date ranges from a list of dates?
I have a list of dates, for example: How do I find the contiguous date ranges contained within those dates? In the above example, the ranges should be: Thanks. Answer This works, but I’m not happy with it, will work on a cleaner solution an edit the answer. Done, here is a clean, working solution: And t…