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

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

Related

Add Days Google Script

I wondered if anyone could help. I have a script where I am pulling out data from a spreadsheet list, where this is a match for this week (basically an events list, to produce a weekly agenda). I will use a for loop to increment the days to add on, but I am just trying to make it work for one day for now...
The first column is the data in format dd/mm/yyy
I am trying to take today's increment by 1 and then search through the list to find a match. The searching etc, I can make work, but the date part is just not playing. I wondered if anyone could advise.
E.g. Date Column A:
06/07/2021
06/07/2021
01/11/2021
01/11/2021
01/11/2021
01/11/2021
02/09/2021
02/09/2021
var selectedDate = row[0];
selectedDate = Utilities.formatDate(new Date(selectedDate), "GMT+1", "dd/MM/yyyy");
var currdate = new Date();
currdate = Utilities.formatDate(new Date(selectedDate), "GMT+1", "dd/MM/yyyy");
var daystochange = 1;
var newdate = new Date(currdate.getFullYear, currdate.getMonth, currdate.getDay+daystochange );
Could anyone help?
Thanks
Only use Utilities.formatDate() to output dates, not to work with dates.
The JavaScript date object has all you need to work with dates and compare. When you use the Utilities function it converts it to a string, and so you lose all the functionality of the Date object.
Also bear in mind that if you have dates, that are formatted as dates in your sheet, they will automatically be returned as Date objects.
For example, if your sheet has a date in cell A1
var date = Sheet.getRange("A1").getValue()
date instanceof Date // true
Once you have your date, if you want to add one day to it, you can take an approach similar to what you have already done:
var selectedDate = new Date(2021, 1, 15)
var newdate = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate() + 1);
console.log(newdate) // Tue Feb 02 2021 00:00:00
Note - use getDate to return the day of the month, getDay only returns day of the week.
To check if two dates are the same, you can write a function to compare:
function isSameDate(a, b) {
return a instanceof Date &&
b instanceof Date &&
a.getYear() === b.getYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
}
This function will return true if the dates are the same.
Reference
Date

Add current timezone difference to timeintervalsince1970 in swift

I have an API call that asks me to convert dates to timeIntervalSince1970 in ms and they also want me to add the correct time zone difference. So my code is at the moment:
if let timeStamp = incident?.publishedTimeStamp as Date? {
let roundedDown = Int(timeStamp.timeIntervalSince1970 * 1000)
publishedDate = String(roundedDown)
}
This gives me a number that if I insert it in here:
https://www.epochconverter.com/
I get this result:
Now as you can see, the GMT time (the top result) is what I at the moment achieve. I need to add enough ms so that the Your time zone section would be in the GMT section. I hope this makes sense.
So how can I get the number in ms that represents the difference between the current time zone from GMT so I can sum it with my current result?
var roundedDown = Double(TimeZone.current.secondsFromGMT(for: timeStamp)) + timeStamp.timeIntervalSince1970
roundedDown *= 1000
publishedDate = String(Int(roundedDown))

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;

Groovy get today's date at minute 00:00:00

I have this problem. I need to do the following:
get todays date
make a new date which will be today's date at 00:00:00
make another date which will be today's date at 23:59:59
For example. Today Date is 12-January-2012 19:00
How can i make a new date, which will be 12-January-2012 00:00 (the start of the current day)
It may seems easy, but i couldnt find any groovyway to get it, any help would be apreciated.
To get the date at midnight use Date.clearTime (docs):
dateAtMidnight = new Date()
dateAtMidnight.clearTime()
(Javadocs are for Groovy JDK < 2.0, clearTime() is declared void in Groovy JDK 2.0, preventing d = new Date().clearTime(). Comments indicate the original functionality may be restored, yay!)
For the comparison, instead of using <= 23:59:59, use < (the next day):
(aDate >= dateAtMidnight) && (aDate < (dateAtMidnight + 1))
An alternative way, but it sets the datetime (but it doesn't get the date merely)
dateAtMidnight = new Date()
dateAtMidnight.set(hourOfDay: 0, minute: 0, second: 0)

How can I compare two dates in vbscript/ASP?

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().