Time comparison in VB.NET - vb.net

I'm trying to see if the current time is between 2 times, but I've been having trouble with this one:
This is my code:
If DATE.NOW < CDate(#6:30:00 AM#) And DATE.NOW >= CDate(#10:30:00 PM#) Then
SHIFT = "NIGHT"
End If
If my time is 11:15 pm I expect to be between the range above, but it doesn't.
More than the formula to get this, i would like to know how the time works in vb.net when you are comparing am vs pm.

You've got to change the condition to OrElse due to that internally dates are just regular numbers (in this case dates are represented by how many microseconds have lapsed since 0001-01-01). Therefore Date.Now cannot possibly be both less than 6:30 AM and more or equal to 10:30 PM.
You've also got to add .TimeOfDay to both sides as doing so will compare only the time of the day, ignoring the date. #6:30:00 AM# will evaluate to 0001-01-01 6:30:00 AM, which is always less than Date.Now (2017-09-25 xx:xx:xx).
This should work:
If Date.Now.TimeOfDay < #6:30:00 AM#.TimeOfDay OrElse Date.Now.TimeOfDay >= #10:30:00 PM#.TimeOfDay Then
This checks if the time is less than 6:30 AM, or more or equal to 10:30 PM.

Related

Parsing date-time into readable formats

I have the following code that WORKS but I am unable to recreate because I do not understand WHY it works. If you plug it in w3schools it compiles successfully.
I do not understand how "1501532100" is parsed into a working date function. individually, I can see how dateadd() and format works, but why does it work the way it does and how can I reverse engineer the rest of the integers into proper dates?
SELECT FORMAT((dateadd(s, 1501532100, '1969-12-31 20:00')), 'MM.dd.yyy');
RETURNS: 07.31.2017
dateadd accepts 3 arguments: interval, number and date. When interval is s, it means that number will be treated as seconds, so it will add that many seconds to the date specified and return the result, which will then be displayed in the MM.dd.yyy format.
You can think of the first argument of dateadd as a measurement unit of the second one.
From: https://www.w3schools.com/sql/func_sqlserver_dateadd.asp
DATEADD(interval, number, date)
interval here is s - seconds,
number is 1501532100
date being 1969-12-31 20:00
all that does is just adds 1501532100 seconds to 1969-12-31 20:00

Date and time range where Date() and Date()-1 can't be use in access query

I am trying to set up a query that pulls data from a date field. The date range (for example) that I need is from 3:40 PM of yesterday to today's date up to 3:40 PM. In other words my day does not star at midnight, so the function Date() can't cover it.
I have set up the query as follows:
Between #2/5/2018 3:40:00 PM# And #2/6/2018 3:39:59 PM#
in a field formatted for general Date (mm/dd/yyyy h:mm:ss AM or PM). With this I would have to change the query every day.
I would like to be able to use the function Date() & Date()-1 to replace today's date (Date()) and Yesterday (Date()-1). How can I do it?
I have also tried having two fields one for the Date (formatted as short date mm/dd/yyyy) when the order was entered, and the time the order was entered (formatted for general Date (mm/dd/yyyy h:mm:ss AM or PM). However when I use the function Date() on the date field and >#h:mm:ss# on the Time field the query yields 0 records.
You can do simple calculus with dates and times. Try the following:
Between Date() - 1 + #3:40:00 PM# And Date() + #3:39:59 PM#

current date with delay

I writing application using vb .net.
In my application I got text box displaying current date.
As usually after midnight date is changing.
There is any chance to set delay that date will change after 02:00 am next day ?
For example:
Today is 09/07/17 and I need that after midnight textbox will still show this date. But after 02:00 am ( 10/07/117 ) date will change for 10/07/17.
My current code:
Private Sub HOMESCREEN_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SMARTSCREEN.MdiParent = Me
SMARTSCREEN.StartPosition = FormStartPosition.CenterScreen
SMARTSCREEN.Show()
SMARTSCREEN.datee.Text = Date.Today()
SMARTSCREEN.NOWEEK.Text = (DatePart("WW", Now))
Thanks,
The big (actually huge) question is, in what time zone is that 2:00 am you're talking about? You can easily achieve that by simply changing the time zone.
Say your time zone is US Eastern Time -5, then setting the time zone in your code to US Mountain Time -7 will effectively give you the result you want.
EDIT: This is trivial, but since you mentioned that you're in UK, then I assume that your time zone is London 0, so set to Coordinated Universal Time -2 to achieve what you want.
Alternatively, it can be simpler in your code to just subtract two hours:
SMARTSCREEN.datee.Text = DateTime.Now.AddHours(-2).ToString("d")

SQL : How sysdate minus a value works?

I found below code in my existing project.
select * from mytable where SomeColumn_date >= trunc(sysdate)-.25/24;
Sample value for SomeColumn_date is 22-JUN-17 05:46:55
How does SomeColumn_date >= trunc(sysdate)-.25/24 work on Date data type?
Different database engines allow different operations to be applied to date data types. For the most part, an operation of <Date Value> +/- 1 will add or subtract one day to that date value. This is syntactically equivalent to the dateadd function when using days.
In your example here, the -.25/24 resolves to the number equivalent of -15 minutes, which is then subtracted from your date value.
It is essentially a much less readable version of datedd(min,-15,<Date Value>).
From the documentation of TRUNC (I'm guessing you are using Oracle):
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. [...] If you omit fmt, then date is truncated to the nearest day.
The result of trunc(sysdate) would be the present date without the time component. Now .25/24 (actually meaning 0.25/24) is substracted from that. If you substract a date using - the operand is always in days. 0.25/24 would be a form to express a quarter of an hour.
So trunc(sysdate)-.25/24 would result in yesterday 23:45.
Ok so 2 things are happening here:
trunk(date,fmt)
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. If you omit fmt, then date is truncated to the nearest day.
So if you have suppose 22-JUN-17 05:46:55 you get 22-JUN-17. Since you don't have the fmt
DATETIME - .25/24 implies .25 hours before your current Date time.
But since you have only DATE all it does is .25 hours before todays 12:00 AM i.e yesterdays 11:45PM
SomeColumn_date >= trunc(sysdate)-.25/24
So suppose if its 22-JUN-2017 right now the date is compared to 21-JUN-2017 11:45 PM
NOTE: - is for before current time, + is for after the current time

Sysdate Oracle to Run Based on Day

This is probably an easy question for most of you but how can I get this mask to run based on just the day?
If anyone knows Crystal Reports syntax, we have this and it works {PO_RECEIPTS.DATE_RECEIVED} = currentdate
However, when converting to Oracle SQL, how can I the standard: TO_CHAR
(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') to become range so we can selected everything during the day, not just what matched the second in which the report was ran which it never will.
So something like Today from 00:00:00 to 23:59:59 ?
Thank you!
If PO_RECEIPTS.DATE_RECEIVED is a date column where all the times are set to midnight then you can do:
WHERE PO_RECEIPTS.DATE_RECEIVED = TRUNC(sysdate)
If the values have other times then you can use a range:
WHERE PO_RECEIPTS.DATE_RECEIVED >= TRUNC(sysdate)
AND PO_RECEIPTS.DATE_RECEIVED < TRUNC(sysdate) + 1
Truncating a date sets the time to midnight, by default, so TRUNC(sysdate) is midnight this morning. For the range you get all records equal to or later than midnight this morning, and less than midnight tomorrow - which is what TRUNC(sysdate) + 1 gives you, using normal Oracle datetime arithmetic.
You don't really want to convert it to a string with TO_CHAR(); you'd either have to convert all the column values to strings too (which is inefficient and prevents an index being used), or let the string be (implicitly) converted back to a date anyway. It's better to compare a column value with the same data type to reduce or avoid confusion.