how to know am or pm in vb - vb.net

im doing my activity dtr system and i want to know how to determine whether it am or pm? 7am - 12:30 pm should go on AM, and 1pm - 5PM will go to pm.
for example:
if the time is 12:30 pm its still go to am and if its 12:31 pm it will go now to pm.
Dim hour As Date = TimeOfDay.ToString("hh:mm")
If hour <= "12:30" Then
isTimeAM_Exist() ' will go to am
Else
isTimePM_Exist() ' will go to pm
End If
in my code 4:00 pm still go in am.
thankyou in advance

Instead of converting to string, work directly with the DateTime structure, and compare it with a correct starting value
Dim now = Date.Now
If now < New DateTime(now.Year, now.Month, now.Day, 12, 00, 0) Then
'It's currently AM
Else
'It's currently PM
End If

sorry when I do not understand your question, but TimeOfDay HAS this Information:
Dim hour As Date = TimeOfDay
If hour.Hour >= 12 Then
' do whatever you like to do on afternoon
Else
' time to wake up
End If
had to test this first in my ide

This will allow you to define your own 'AM' and 'PM':
' FormatDateTime(Now(), 4) gives 24-hour format (hh:mm)
If Hour(FormatDateTime(Now(), 4)) + Minute(FormatDateTime(Now(), 4))/60 <= 12.5 Then
' Is AM
Else
' Is PM
End If

maybe this way is more effective
MsgBox(DateTime.Now.AddMinutes(-30).ToString("tt"))

Related

Need to generate a range of date time between dates but time start and end at 02:00:00 am

Hello I am trying to generate a date time range between the two dates with an increment of 15 minutes, however I want it to start at 02:00:00 am of the start date and end at 02:00:00 am of the end date. The start and end dates should coming from date picker on my dashboard in the future.
I know I can generate the date time range using the below command however I have not been able to start and end at 02:00:00 am.
dates = pd.date_range(start='1/1/2018', end='1/2/2018', freq='15min')
I also found on one of the stack overflow articles to force the date range to start at and end at 02:00:00 am, however I am not able to figure out how to pass the dates as they would be in dates format from the date picker but below they are just numbers
dts = [dt.strftime('%Y-%m-%d %H:%M') for dt in
datetime_range(datetime(2018, 6, 3,2), datetime(2018, 6, 10,2),
timedelta(minutes=15))]
I am hoping if some one can please help to resolve this issue.
P.S. I am trying to figure out if it is possible before I make changes to the frontend and use date picker instead.
Thanks a lot in advance !!
IIUC
d1='1/1/2018'
d2='1/2/2018'
pd.date_range(start=d1+ ' 02:00:00', end=d2+' 02:00:00', freq='15min')

Time durations of the day SQL

I have time durations:
11-09-2018 10:00 AM - 11-09-2018 12:00 PM
11-09-2018 4:00 PM - 11-09-2018 8:00 PM
I have a requirement to write a SQL query to derive the other remaining durations for the day 11-09-2018. My query output should be:
11-09-2018 12:00 AM - 11-09-2018 9:59 AM
11-09-2018 12:01 PM - 11-09-2018 3:59 PM
11-09-2018 08:01 PM - 11-09-2018 11:59 PM
please use this for your requirement as general, it is useful when you want to give any date OR datetime as input and want output as given above then you can use following query. Put any date or datetime(timestamp) in place of getdate() and you will get the required output.
PRINT CONVERT(VARCHAR,dateadd(hh,00,datediff(dd,0,GETDATE())),100) + ' - ' + CONVERT(VARCHAR,dateadd(mi,59,dateadd(hh,09,datediff(dd,0,GETDATE()))),100) +' '+ char(13) + char(10) + CONVERT(VARCHAR,dateadd(mi,01,dateadd(hh,12,datediff(dd,0,GETDATE()))),100) + ' - ' + CONVERT(VARCHAR,dateadd(mi,59,dateadd(hh,15,datediff(dd,0,GETDATE()))),100) +' '+ char(13) + char(10) + CONVERT(VARCHAR,dateadd(mi,01,dateadd(hh,20,datediff(dd,0,GETDATE()))),100) + ' - ' + CONVERT(VARCHAR,dateadd(mi,59,dateadd(hh,23,datediff(dd,0,GETDATE()))),100)
Since you haven't provided DDL, I won't write a query, but I will give you an approach you can use.
You need to do some kind of UNION to account for the fact that you may need to create rows both before your first row AND after your last row.
One way you can do it is a "generate a row before each existing row, UNIONed with a last row"
Using a self-join (OUTER) to get a reference to the previous row, SELECT one minute after the previous row's end time as the start time (COALESCE NULL with '12 AM'), and one minute before the current row's start time as the end time. Use a WHERE clause to filter out any row with a start time of '12:00 AM'.
This will give you, for each row in your table, the time range before it that you want.
Then you UNION that with a final row, consisting of one minute after the last end time as the start time and '11:59 PM' as the end time. Use a WHERE clause to filter this row out if the last end time is '11:59 PM'.

Time comparison in 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.

vb.net add days to a date

Dim DespatchDate As DateTime
Dim ReceiptDate As DateTime
DespatchDate = #3/7/2017 12:00:00 AM# 'I am in the UK so this is 3rd July 2017
ReceiptDate = DespatchDate.AddDays(60) 'Returns #5/6/2017 12:00:00 AM#
I would expect "1/9/2017" the 1st September 2017
I also tried
ReceiptDate = DateAdd("d", 10, DespatchDate) which returned #3/17/2017 12:00:00 AM#
I must be doing something wrong but what?
The date literal in VB uses the US format regardless of where you are. Your code:
DespatchDate = #3/7/2017 12:00:00 AM# 'I am in the UK so this is 3rd July 2017
is creating the date March 7 2017. Read more on date literals or use the constructor on the date class that makes it more apparent what you are doing.
Update
In order to be sure what the date is and to avoid ambiguity, you can define it this way:
DespatchDate = New Date(2017, 7, 3) ' Without the time of day, 12AM is implied
DespatchDate = New Date(2017, 7, 3, 12, 34, 56) ' July 3 2017 12:34:56 PM
The good thing with this is you can see from intellisense while typing the code what each number means. And if someone else had written the code, you can invoke intellisense to see what each argument means. As you can see from what I highlighted in my screenshot, the time is in 24 hour format.

Formula for CurrentDate with Set Time interval

I have a requirement to pass a parameter that calls the CurrentDate for a fixed time of day. Example:
StartDate = CurrentDate, at 8:00 am
End Date = CurrentDate, as 5:00 pm
Any suggestions on how this can be done would be greatly appreciated!!
I haven't time to do anything in-depth but my first thought is to manipulate DateTime2FromParts
https://msdn.microsoft.com/en-us/library/hh213312.aspx
You can get the parts of the date using
select DATEPART(dd, getdate())
... or replace dd with mm for month and so on.
Hope that helps