Creating a range of dates in Python
Marginally better...
base = datetime.datetime.today()
date_list = [base - datetime.timedelta(days=x) for x in range(numdays)]
Javascript - get array of dates between 2 dates
function (startDate, endDate, addFn, interval) {
addFn = addFn || Date.prototype.addDays;
interval = interval || 1;
var retVal = [];
var current = new Date(startDate);
while (current <= endDate) {
retVal.push(new Date(current));
current = addFn.call(current, interval);
}
return retVal;
}
Get all dates between two dates in SQL Server
My first suggestion would be use your calendar table, if you don't have one, then create one. They are very useful. Your query is then as simple as:
DECLARE @MinDate DATE = '20140101',
@MaxDate DATE = '20140106';
SELECT Date
FROM dbo.Calendar
WHERE Date >= @MinDate
AND Date < @MaxDate;
If you don't want to, or can't create a calendar table you can still do this on the fly without a recursive CTE:
DECLARE @MinDate DATE = '20140101',
@MaxDate DATE = '20140106';
SELECT TOP (DATEDIFF(DAY, @MinDate, @MaxDate) + 1)
Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @MinDate)
FROM sys.all_objects a
CROSS JOIN sys.all_objects b;
For further reading on this see:
- Generate a set or sequence without loops – part 1
- Generate a set or sequence without loops – part 2
- Generate a set or sequence without loops – part 3
With regard to then using this sequence of dates in a cursor, I would really recommend you find another way. There is usually a set based alternative that will perform much better.
So with your data:
date | it_cd | qty
24-04-14 | i-1 | 10
26-04-14 | i-1 | 20
To get the quantity on 28-04-2014 (which I gather is your requirement), you don't actually need any of the above, you can simply use:
SELECT TOP 1 date, it_cd, qty
FROM T
WHERE it_cd = 'i-1'
AND Date <= '20140428'
ORDER BY Date DESC;
If you don't want it for a particular item:
SELECT date, it_cd, qty
FROM ( SELECT date,
it_cd,
qty,
RowNumber = ROW_NUMBER() OVER(PARTITION BY ic_id
ORDER BY date DESC)
FROM T
WHERE Date <= '20140428'
) T
WHERE RowNumber = 1;
Python generating a list of dates between two dates
You can use pandas.date_range()
for this:
import pandas
pandas.date_range(sdate,edate-timedelta(days=1),freq='d')
DatetimeIndex(['2019-03-22', '2019-03-23', '2019-03-24', '2019-03-25',
'2019-03-26', '2019-03-27', '2019-03-28', '2019-03-29',
'2019-03-30', '2019-03-31', '2019-04-01', '2019-04-02',
'2019-04-03', '2019-04-04', '2019-04-05', '2019-04-06',
'2019-04-07', '2019-04-08'],
dtype='datetime64[ns]', freq='D')
How to get all the dates between two dates?
An alternative solution to your problem is using pandas
import pandas as pd
pd.date_range(start=start_date,end=end_date)
Print all day-dates between two dates
I came up with this:
from datetime import date, timedelta
start_date = date(2008, 8, 15)
end_date = date(2008, 9, 15) # perhaps date.now()
delta = end_date - start_date # returns timedelta
for i in range(delta.days + 1):
day = start_date + timedelta(days=i)
print(day)
The output:
2008-08-15
2008-08-16
...
2008-09-13
2008-09-14
2008-09-15
Your question asks for dates in-between but I believe you meant including the start and end points, so they are included. To remove the end date, delete the "+ 1" at the end of the range function. To remove the start date, insert a 1 argument to the beginning of the range function.
Iterating through a range of dates in Python
Why are there two nested iterations? For me it produces the same list of data with only one iteration:
for single_date in (start_date + timedelta(n) for n in range(day_count)):
print ...
And no list gets stored, only one generator is iterated over. Also the "if" in the generator seems to be unnecessary.
After all, a linear sequence should only require one iterator, not two.
Update after discussion with John Machin:
Maybe the most elegant solution is using a generator function to completely hide/abstract the iteration over the range of dates:
from datetime import date, timedelta
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)
start_date = date(2013, 1, 1)
end_date = date(2015, 6, 2)
for single_date in daterange(start_date, end_date):
print(single_date.strftime("%Y-%m-%d"))
NB: For consistency with the built-in range()
function this iteration stops before reaching the end_date
. So for inclusive iteration use the next day, as you would with range()
.
Python Find a day dates between a range of two dates
from datetime import date, timedelta, datetime
import time
firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'
firstDay = datetime.strptime(firstDay, '%d-%B-%Y')
lastDay = datetime.strptime(lastDay, '%d-%B-%Y')
dates = [firstDay + timedelta(days=x) for x in range((lastDay-firstDay).days + 1) if (firstDay + timedelta(days=x)).weekday() == time.strptime(weekDay, '%A').tm_wday]
output
dates
[datetime.datetime(2000, 1, 3, 0, 0),
datetime.datetime(2000, 1, 10, 0, 0),
datetime.datetime(2000, 1, 17, 0, 0),
datetime.datetime(2000, 1, 24, 0, 0),
datetime.datetime(2000, 1, 31, 0, 0),
datetime.datetime(2000, 2, 7, 0, 0),
datetime.datetime(2000, 2, 14, 0, 0),
datetime.datetime(2000, 2, 21, 0, 0)]
The output in format weekDay-Month-year
[d.strftime("%A-%B-%Y") for d in dates]
['Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-February-2000',
'Monday-February-2000',
'Monday-February-2000']
output in firstDay
format: 1-January-2000
[d.strftime("%-d-%B-%Y") for d in dates]
['3-January-2000',
'10-January-2000',
'17-January-2000',
'24-January-2000',
'31-January-2000',
'7-February-2000',
'14-February-2000',
'21-February-2000']
Get all Dates between 2 Dates Javascript
you are on the right way
setHours
could have an issue with timezone try to use setUTCHours
;
and reset start and end dates before any calculations
try out:
function getDatesInRange(startDate, endDate) {
const start = new Date(new Date(startDate).setUTCHours(0, 0, 0, 0));
const end = new Date(new Date(endDate).setUTCHours(0, 0, 0, 0));
const date = new Date(start.getTime());
const dates = [];
while (date <= end) {
dates.push(new Date(date));
date.setDate(date.getDate() + 1);
}
return dates;
}
const travel_start = "2022-03-20T05:59:57.118Z";
const travel_start2 ="2022-03-20T23:59:57.118Z";
const travel_end = "2022-03-23T20:00:57.118Z";
const res1 = getDatesInRange(travel_start, travel_end);
const res2 = getDatesInRange(travel_start2,travel_end);
console.log('res1', res1)
console.log('res2', res2)
Related Topics
Does Ms Access Support "Case When" Clause If Connect with Odbc
Could Not Find Stored Procedure 'Dbo.Aspnet_Checkschemaversion'
Select for Update with SQL Server
How to Convert Comma Separated Nvarchar to Table Records in SQL Server 2005
SQL Where Id in (Id1, Id2, ..., Idn)
What Does the Colon Sign ":" Do in a SQL Query
How to Have an Indexed View in MySQL
How to Reorder Rows in SQL Database
How to Include Null Values in a Min or Max
Mysql, Iterate Through Column Names
@@Identity, Scope_Identity(), Output and Other Methods of Retrieving Last Identity
SQL Server Convert Integer to Binary String
In SQL/Mysql, Differencebetween "On" and "Where" in a Join Statement
The Parameterized Query Expects the Parameter Which Was Not Supplied
How to Have Nhibernate Only Generate the SQL Without Executing It