Time comparison and difference in vb - vb.net

How to compare between current time and a past time in vb and check if the duration time is less than 30 mins or not ?
Example :
Current time : 11:30am
Time : 11:00am
Duration : 30 min (time should be less than 30 mins. )
If the time duration is exactly 30 min difference
Return true
Else false

If (Date.Now - myDateTime) < TimeSpan.FromMinutes(30) Then
'Time difference is less than 30 minutes.
End If
In that case, myDateTime may have been the value of Date.Now that you stored earlier or it may have been constructed in some other way.
Hmmm... I read the question again and it seems that you contradict yourself. First you say that you want to know whether the difference is less than 30 minutes and then you say that you want to return True if the difference is exactly 30 minutes. Perhaps you could make up your mind and get back to us because it's hard to provide a single answer to two contradictory questions.

Related

Basic DateTime Algebra

How do i go about writing some basic algebra math in vb.net?
Example:
I need to schedule a process to run 60 days before start date of an object.
When my process runs I do:
Now(UTC) - StarDate(UTC) = days diff
If days diff less than 60, i run my process immediately.
If its over 60 days, i need to schedule my process to run again in 60 days.
So this leaves me with:
X - [daysDiff] = 60
I know how to solve this on paper but not in vb.net code.
Example:
X - 107 = 60
add 107 to each side, i have my # of days to add (107)
How do i accomplish this in VB.net?
If you want to know when to run it: if it's over 60 days, then subtract 60 from that value, that will tell you when when to check again.... so if you do the initial check and it comes back 61 days different, it's over 60, so you subtract 60, leaving 1. So now you know you need to schedule it in 1 day. If you do the initial check and it comes out 107, you subtract 60 and the result is 47... so you know in 47 days, you need to run it again as that will be 60 days out. It's two simple steps.

Sql Query information

What does this do in a SQL Query? Can someone explain? What does the .5- represent?
WHERE ScheduleEntry.ScheduleDate >= getdate() and ScheduleEntry.ScheduleDate <= getDate() +.50
Think of date unit as 1 day. 0.50 of a day is 1/2 of a day. So this returns anything that has ScheduleDate within half a day from getdate() time forward.
It restricts the rows returned to rows where the ScheduleEntry.ScheduleDate is in the future and where it is not more than .50 units later than the current date. To find out how much time .50 units is equal to, run the following on your console:
SELECT getDate()
SELECT getDate() +.50
The difference between the dates should tell you the difference. Most likely, it is half a day.
GETDATE returns the current date and time in SQL.
You can use addition to "add days" to the current date.
Generally, it is better to use DATE_ADD instead of adding directly and is probably easier to read. Adding 0.5 is akin to adding half a day (or 12 hours).
To better illustrate, the .5 is half a day
SELECT DateDiff(HH,getDate(),getDate() +.50)
Returns 12 hours

sql Sum time that occurs in an interval

i have a lot of data that has at start time and a finnish time. These are formated i datetime format.
i want to sum the time that occurs in an timeinterval
if specify the time interval 08-11
i only want to get the time between these to even if the evvent progresses from 06 to 12
If you are using SQL Server you could do it like that:
SELECT SUM(DATEDIFF(HOUR,StartTimeColumn,EndTimeColumn)) AS ElapsedHoursTotal,
SUM(DATEDIFF(MINUTE,StartTimeColumn,EndTimeColumn)) AS ElapsedMinutesTotal,
SUM(DATEDIFF(SECOND,StartTimeColumn,EndTimeColumn)) AS ElapsedSecondsTotal,
FROM dbo.YourTable
You will have to find the perfekt interval (First Parameter of DATEDIFF Function) for your requirements... Hours, Minutes, Seconds, Nanoseconds,...
Im using mssql 2012
the problem is that i can get the full elapsed time from start to finnish but I only want the part that matches my search
if the pattern i match for is 8-11
thing one 08-12 should produce 3 hours
thing two 10-11 should produce 1 hour
thing tre 9.30- 14 shoud produce 1.5 hour

Firebird - Converting stored integer to time equivalent

I am currently working on a Firebird database which stores slots for a time (like appointments). In the data the timeslot is stored in an Integer format but is not the slot time.
Example:
Slot Time: 11am
Database Value: 660
The database value is represented as the number of minutes since midnight. So 11am is 660 minutes from midnight. Or 12noon is 720
Question How, in Firebird, can a convert this 660 to display as 1100 (still an integer) or 540 as 900 (basically 9am)?
What you have stored into database is minutes since start of the day. So you just divide with 60 to get hours. Keep in mind that in SQL if both arguments of the division are integers you'll get integer as an answer, not decimal! Something like following should work for you (assuming the number of minutes is stored to timeslot field):
SELECT
cast(timeslot / 60 as varchar(2)) ||
case
when mod(timeslot, 60) < 10 then '0'|| mod(timeslot, 60)
else mod(timeslot, 60)
end
FROM t
This should give you 1130 for 11:30 am, not 1150 (11,5 hours).
Also see the DATEADD function.
First off I'd store it in the database as a TIME column instead of a number of minutes. Note that on a technical level Firebird stores TIME as the number of 100 microseconds since midnight. However if you really want to store as number of minutes, then you can use:
From minutes to TIME using DATEADD:
DATEADD(x MINUTE TO TIME'00:00')
Time to minutes using DATEDIFF:
DATEDIFF(MINUTE FROM TIME'00:00' TO y)
So:
SELECT
DATEADD(660 MINUTE TO TIME'00:00'),
DATEDIFF(MINUTE FROM TIME'00:00' TO TIME'11:00')
FROM RDB$DATABASE
Will return: 11:00:00.000, 660

Battling Datediff in SQL

I am writing a little query in SQL and am butting heads with an issue that it seems like someone must have run into before. I am trying to find the number of months between two dates. I am using an expression like ...
DATEDIFF(m,{firstdate},{seconddate})
However I notice that this function is tallying the times the date crosses the monthly threshold. In example...
DATEDIFF(m,3/31/2011,4/1/2011) will yield 1
DATEDIFF(m,4/1/2011,4/30/2011) will yield 0
DATEDIFF(m,3/1/2011,4/30/2011) will yield 1
Does anyone know how to find the months between two dates more-so based upon time passed then times passed the monthly threshold?
If you want to find some notional number of months, why not find the difference in days, then divide by 30 (cast to FLOAT as required). Or 30.5-ish perhaps - depends on how you want to handle the variable month length throughout the year. But perhaps that's not a factor in your particular case.
The following statements have the same startdate and the same endate. Those dates are adjacent and differ in time by .0000001 second. The difference between the startdate and endate in each statement crosses one calendar or time boundary of its datepart. Each statement returns 1. ...
SELECT DATEDIFF(month, '2005-12-31 23:59:59.9999999'
, '2006-01-01 00:00:00.0000000'); ....
(from DATEDIFF, section datepart Boundaries ). If you are not satisfied by it, you probably need to use days as unit as proposed by martin clayton
DATEDIFF(m,{firstdate},ISNULL({seconddate},GETDATE())) - CASE
WHEN DATEPART(d,{firstdate}) >= DATEPART(d,ISNULL({seconddate},GETDATE()))
THEN 1
ELSE 0
DATEDIFF is like this by design. When evaluating a particular time measurement (like months, or days, etc.), it considers only that measurement and higher values -- ignoring smaller ones. You'll run into this behavior with any time measurement. For example, if you used DATEDIFF to calculate days, and had one date a few seconds before midnight, and another date a few seconds after midnight, you'd get a "1" day difference, even though the two dates were only a few seconds apart.
DATEDIFF is meant to give a rough answer to questions, like this:
Question: how many years old are you?
Answer: some integer. You don't say "I'm 59 years, 4 months, 17 days, 5 hours, 35 minutes and 27 seconds old". You just say "I'm 59 years old". That's DATEDIFF's approach too.
If you want an answer that's tailored to some contextual meaning (like your son who says "I'm not 8! I'm 8 and 3-quarters!, or I'm almost 9!), then you should look at the next-smallest measurement and approximate with it. So if it's months you're after, then do a DATEDIFF on days or hours instead, and try to approximate months however it seems most relevant to your situation (maybe you want answers like 1-1/2 months, or 1.2 months, etc.) using CASE / IF-THEN kinds of logic.