This question already has answers here:
Generate a list of datetimes between an interval
(5 answers)
Closed 8 years ago.
I have a starting date (lets say January 5th 2014) and an end date (lets say July 10th 2014). I know that an event occurs every Wednesday. Is there an easy way in Python to output all Wednesday with date between those date ranges?
So assuming January 7th is a Wednesday, then the code snippet would output 01.07.2014, 01.14.2014, 01.21.2014, and so on.
To get the first specific weekday after a given date, just add a timedelta of the difference in the weekdays:
wed = 2 # from the datetime.weekday definition
first_wed = start + datetime.timedelta(days=(7 + wed - start.weekday()) % 7)
Once you have that, please see Generate a list of datetimes between an interval in python.
Related
This question already has answers here:
Pandas: How to create a datetime object from Week and Year?
(4 answers)
Closed 4 months ago.
I have the following data:
week = [202001, 202002, 202003, ..., 202052]
Where the composition of the variable is [year - 4 digits] + [week - 2 digits] (so, the first row means it's the first week of 2020, and so on).
I want to transform this, to a date-time variable [YYYY - MM - DD]. I'm not sure what day could fit in this format :( maybe the first saturday of every week.
week_date = [2020-01-04, 2020-01-11, 2020-01-18, ...]
It seems like a simple sequence, neverthless I have some missings values on the data, so my n < number of weeks of 2020.
The main purpose of this conversion is that I can have a fit model to train in prophet. I also think I need no missing values when incorporating the data into prophet, so maybe the answer could be also adding 0 to my time series?
Any ideas? Thanks
Try:
l = [202001, 202002, 202003, 202052]
out [datetime.datetime.fromisocalendar(int(x[:4]), int(x[4:]), 6).strftime("%Y-%m-%d") for x in map(str,l)]
print(out)
outputs:
['2020-01-04', '2020-01-11', '2020-01-18', '2020-12-26']
Here I used 6 as the week day but chose as you want
This makes a datetime object from the first and last part of each number after mapping them to a string, then outputs a string back with strftime and the right format.
Is there a way to deduct a specified number of months from a given date. So for example, if the day is 2006/02/27. Then I want to backtrack 3 months and find the months within this date and 3 months back. In this case, it would be Feb, Jan & dec. What I am really after is finding a range of months.
I can think of using timedelta and specifying 93 days (31 x 3). But this could potentially be a problem if its early month date. something like 01/03/2006 - 93 days will perhaps result in a date in november/2005, which will include march, Feb,Jan,Dec,Nov as months. But what I want is March,Feb and Jan
from datetime import datetime,timedelta
someDate = datetime(2006,2,27)
newDate =someDate - timedelta(days = 3)
#someDate - 3months
Any ideas on how to solve?
This question already has answers here:
Getting the date of the first day of the week
(2 answers)
Python: give start and end of week data from a given date
(5 answers)
Closed 3 years ago.
I am trying to get the timestamp of monday at 00:00 of the current week in python. I know that for a specific date, the timestamp can be found using
baseTime = int(datetime.datetime.timestamp(datetime.datetime(2020,1,1)))
However, I want my program to automatically find out, based on the date, which date monday of the current week was, and then get the timestamp. That is to say, it would return different dates this week and next week, meaning different timestamps.
I know that the current date can be found using
import datetime
today = datetime.date.today()
Thanks in advance
I am trying to get the timestamp of monday at 00:00 of the current week in python
You could use timedelta method from datetime package.
from datetime import datetime, timedelta
now = datetime.now()
monday = now - timedelta(days = now.weekday())
print(monday)
Output
2020-01-27 08:47:01
This question already has answers here:
Find objects between two dates MongoDB
(17 answers)
Mongodb query specific month|year not date
(5 answers)
Closed 4 years ago.
I am trying to write a mongo query in python to get the monthly and weekly range from days.
So far I have
my_collection.find({
'date':{
'$gte': date_start,
'$lte': date_end
}
}
It returns the day range between date_start and date_end, I tried to use '$month': 'gte', 'lte' but is not working, does anyone has any ideas how to get monthly and weekly based on the start date and end? The date_start and date_end are datetime objects.
Example ( date_start = 2017-01-01, date_end = 2017-12-01) I want to return just the month between this two range(start, end): 01, 02, 03, 04, 05, 06 ..
I just went through the same examples that has been asked before and what I asked is different, in my situation I have two dates, start and end.. now from this two dates I just want to display the month by combining the two dates together.. so far I haven't seen an example of how to do it.
Thanks
This question already has answers here:
Checking date against date range in Python
(5 answers)
Closed 4 years ago.
new programmer just getting started with Python.
How would I be able to print out a list of timestamps, every second, given a date range, for example 4/12/17 through 4/21/17 in date -d format?
The output would be something along the lines of
Wed Apr 12 00:00:00 CST 2017
Wed Apr 12 00:00:01 CST 2017
.
.
.
Fri Apr 21 00:00:00 CST 2017
It would ideally be in a nice, vertical list for the output.
I am guessing the solution involves nested for loops, but I just can't wrap my head around the process of making it happen.
Thanks for the help!
Use datetime: https://docs.python.org/3/library/datetime.html
At a high level, I would start with initializing a datetime object with the first timestamp in your range, and a 1-second timedelta object.
import datetime
myDatetime = datetime.datetime(# TODO)
myTimedelta = datetime.timedelta(# TODO)
Then you could use a while loop and an endDatetime object with your last time in the range:
endDateTime = datetime.datetime(# TODO)
while myDatetime <= endDatetime:
print(myDatetime) # need to format it to look like your example
myDatetime = myDatetime + myTimedelta
Or you can calculate how many seconds are between the first and last end time and use a for loop:
seconds = # TODO
for i in range(seconds+1):
print(myDatetime + (i * myTimedelta))