How can I compare two dates in vbscript/ASP? - date

Using ASP classic, I need to somehow compare two dates with each other. How can I do this?

Date1 = #rs["date"]#
Date2 = #12/1/2009#
If DateDiff("d", Date1, Date2) > 1 Then
response.write "This date is before 12/1/2009"
Else
response.write "This date is after 12/1/2009"
End If

If Date1 > Date2 Then
' Date1 occurred after Date 2
End If
Use >, < and = like comparing numbers (and >=, <= and <> too). Smaller dates are more historic.
This of course assumes that Date1 and Date2 are actually Date or DateTime objects. If they aren't, you'll need to convert them to Date objects first using CDate().

Related

dlookup not working for other month than MAY

I am using dlookup (date = date) its fine when the date is of "May" but when I put date of any other month like 1/6/19 the dlookup remains silent.
in Dlookup if date is of MAY its fine but not working when date id of JUN the code I am using is as below
Me
Total = DLookup("Total", "Dailycash", "Cdate =#" & sdate & "#")
Apply a proper format to the date value expression:
Total = DLookup("Total", "Dailycash", "Cdate = #" & Format(sdate, "yyyy\/mm\/dd") & "#")

Better way to make/compare date ranges?

I often have data that has a date1 and a date2. Date1 is the date we guess will have the event and date2 an event. I usually need to make 2 dummy variables where I increment date1 forwards a week and backwards then compare with the other 2. However I keep thinking there must be a better way to create a date range and then compare with a second date!
Is there a way to do this in sas? Basically I want to take date1 and date2 and make this dataset and am wondering if I MUST create 2 additional variables (date1-7 days and date1+7days)
Input datset:
DATE1 DATE2
10/23/2014 2/12/2015
2/12/2015 2/10/2015
Current output:
DATE1_wk_before Date1_wk_after Date2 In_range_indicator
10/16/2014 10/30/2014 2/12/2015 0
2/05/2015 2/19/2015 2/10/2015 1
Where In_range_indicator = 1 if date is in the range and 0 if not in the range
I want to know if I can do it just where I do something like
In_range_indicator= 1 where Date2 is in range(week before date1 , week after date1) without creating 2 extra sets of data. It seems a waste of time.
I am LITERALLY adding 7 days and subtracting 7 days before and after and it seems a bad way to do this.
You seems to just want to set the value of a variable based on a condition. No need to get too clever with it, just if and else in your data step:
if date2 ge date1-7 and date2 le date1+7 then ind=1;
else ind=0;
Agree with #DWal, simple if and else statement can help. You can also use IFN function.
data mydates;
infile datalines missover;
input (date1-date2) (:mmddyy10.);
In_range_indicator=ifn( date1-7 <= date2 <= date1+7 , 1,0);
format date1-date2 yymmdd10.;
datalines4;
10/23/2014 2/12/2015
2/12/2015 2/10/2015
;;;;
run;
proc print data=mydates;run;
if abs(date2-date1)<7 then ind=1; else ind=0;

C# code to find whether today Date lies between or equal to the two other specified date

I have two date as
Date1= 7/28/2014 and Date2=7/31/2014.
I want to check whether today date i.e 7/31/2014 lies between the above mentioned data.
Try this :)
DateTime dat1 = Convert.ToDateTime("7/28/2014");
DateTime dat2 = Convert.ToDateTime("8/1/2014");
DateTime today = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyy"));
if (today > dat1 && today < dat2)
{
// between
}
else
{
// lies between
}
Maybe your problem lays in Time part of DateTime...if Date2 is 7/31/2014 0:00:00 and Now is 7/31/2014 11:30:00 then Date2 < Now...consider using DateTime::Date property

Classic ASP - Display all dates within a certain date range

I was wondering if i could get some help.
I have a little script that gets a date range based on a persons input.
It outputs the date range like so
6/7/2014
6/27/2014
What i need to do is output each date between these 2 dates.
Any help would be greatly appreciated.
Cheers
Dates in Classic ASP are fairly easy to use. Try this first: http://www.classicasp.aspfaq.com/date-time-routines-manipulation/could-i-get-a-little-help-with-dates.html
Here's a demo of looping through a series of sequential dates:
dim dcount, newdate
dcount = 0
newdate = startdate
do
response.write(newdate & "<br />")
newdate = newdate + 1
loop until newdate > stopdate
Obviously you'd need to specify startdate and stopdate, though this could be done as a sub or function.

TSQL DateTime Comparison

What I am trying to do is get a result from sql where the dates are in a certain range but its not working correctly, here is my query.
DECLARE #CurrDate DATETIME
SET #CurrDate = GETDATE()
SELECT dbo.ProductDetails.PartnerID
,dbo.ProductDetails.ProductID
,dbo.Products.ProductName
,StartDate
,EndDate
FROM dbo.ProductDetails
INNER JOIN dbo.Products
ON dbo.ProductDetails.ProductID = dbo.Products.ProductID
WHERE CONVERT(VARCHAR(10),StartDate,111) <= #CurrDate
AND CONVERT(VARCHAR(10),EndDate, 111) >= #CurrDate
but when the Enddate = #CurrDate the row does not show, but if i make that date just one day higher it gets displayed. Am i doing anything wrong? Any advice will do, thanks.
GetDate() returns date and time, while your conversion to varchar strips away the time part (I'm suspecting that's all it's actually supposed to do). So you would need to do the same conversion for #CurrDate.
If what you want is to simply consider the date only (ignoring the time part), you could use DATEDIFF instead of converting to varchar (see here); example:
DECLARE #CurrDate DATETIME
SET #CurrDate = GETDATE()
SELECT dbo.ProductDetails.PartnerID, dbo.ProductDetails.ProductID,
dbo.Products.ProductName , StartDate, EndDate
FROM dbo.ProductDetails INNER JOIN
dbo.Products ON dbo.ProductDetails.ProductID = dbo.Products.ProductID
-- where StartDate is on the same day or before CurrDate:
WHERE DATEDIFF(day, StartDate, #CurrDate) >= 0 AND
-- and where EndDate is on the same day or after CurrDate:
DATEDIFF(day, EndDate, #CurrDate) <= 0
If you want only DATE comparison, without time use the
cast(CONVERT(varchar, StartDate, 112) as datetime)
I am quite sure that the comparison takes into account the time as well as the date, in which case if the dates are the same but the current time is greater than the time being compared to you won't get that row as a result.
So, what you need to do is just extract the date part and compare those.
GETDATE() gives you date and time
if yours column have only date
then
CONVERT(VARCHAR(10),StartDate,111) <= #CurrDate
can give you unexpected result
remember
19.12.2011 14:41 > 19.12.2011 00:00
If you are using SQL 2008 or later, and wanting to compare only the date, not the time, you can also do:
Cast(StartDate as Date)
(This avoids having to convert to a string.)