Subtract Time in vb.net - vb.net

Hi guys i need help to subtract the time. 7:31:52 AM - 4:30:32 the ouput is 3hrs 1 mins 20 sec
Dim date1 = New DateTime(Now.Year, Now.Month, Now.Day, 7, 30, 0, 0)
Dim date2 = DateTime.Parse("4:30:00 PM")
If read1("clog") Is (DBNull.Value) Then
Else
date1 = read1("clog")
Dim duration As Double = (date2 - date1).Minutes
Label4.Text = duration
End If

Dim time1 As DateTime = #7/20/2016 7:31:52 AM#
Dim time2 As DateTime = #7/20/2016 4:30:00 PM#
Dim ts As TimeSpan = time2 - time1
MsgBox(ts.Days & " day(s) " & ts.Hours & " hour(s) " & _
ts.Minutes & " minute(s) " & ts.Seconds & " second(s)")
Dim date1 As DateTime
Dim date2 = DateTime.Parse("4:30:00 PM")
If read1("clog") Is (DBNull.Value) Then
Else
date1 = read1("clog")
Dim duration As TimeSpan = date2 - date1
Label4.Text = String.Format("{0} hour(s) {1} minute(S) {2} second(s)", duration.Hours, duration.Minutes, duration.Seconds
End If

Related

Calculate the number of workdays between two dates in HOURS

I have a code that I found on microsoft website :
Function Work_Days(BegDate As Variant, EndDate As Variant) As Integer
Dim WholeWeeks As Variant
Dim DateCnt As Variant
Dim EndDays As Integer
On Error GoTo Err_Work_Days
BegDate = DateValue(BegDate)
EndDate = DateValue(EndDate)
WholeWeeks = DateDiff("w", BegDate, EndDate)
DateCnt = DateAdd("ww", WholeWeeks, BegDate)
EndDays = 0
Do While DateCnt <= EndDate
If Format(DateCnt, "ddd") <> "Sun" And _
Format(DateCnt, "ddd") <> "Sat" Then
EndDays = EndDays + 1
End If
DateCnt = DateAdd("d", 1, DateCnt)
Loop
Work_Days = WholeWeeks * 5 + EndDays
Exit Function
Err_Work_Days:
' If either BegDate or EndDate is Null, return a zero
' to indicate that no workdays passed between the two dates.
If Err.Number = 94 Then
Work_Days = 0
Exit Function
Else
' If some other error occurs, provide a message.
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
End Function
It works fine, but I would like to get the difference in hours, but it's not working. I Changed the "d" for "h", but instead of giving me the exact hours, it's giving me 24 hours.
When I change "d" for "h" and multiply 1 by 24, and 5 by 24, I dont get the exact difference in hours. This is what I get :
Date1 Date2 DIFFERENCE
2022-05-05 09:05:19; 2022-05-05 15:45:14; 24
it's giving me 24h instead of 6h
The Work_Days function returns the number of weekdays as a whole number. Time is not considered.
This code returns weekday hours between two dates with a time component.
Option Explicit
Function Weekday_Hours(BegDateTime As Variant, EndDateTime As Variant) As Long
' Weekday hours between two dates with a time component
Dim WholeWeeks As Long
Dim DateCurrent As Date
Dim BegDate As Date
Dim EndDate As Date
Dim BegTime As Date
Dim EndTime As Date
Dim EndDays As Long
Dim Week_Days As Long
Dim FirstDay_Hours As Long
Dim LastDay_Hours As Long
Dim FirstDay_Minutes As Long
Dim LastDay_Minutes As Long
' If the weekend, move the start date to Monday 12AM
If Weekday(BegDateTime) = vbSaturday Then ' 7 - Saturday
Debug.Print Weekday(BegDateTime) & ": Saturday"
BegDateTime = DateAdd("d", 1, BegDateTime)
BegDateTime = Format(BegDateTime, "yyyy-mm-dd")
Debug.Print " BegDateTime:" & Format(BegDateTime, "yyyy-mm-dd hh:nn:ss")
End If
If Weekday(BegDateTime) = vbSunday Then ' 1 - Sunday
Debug.Print Weekday(BegDateTime) & ": Sunday"
BegDateTime = DateAdd("d", 1, BegDateTime)
BegDateTime = Format(BegDateTime, "yyyy-mm-dd")
Debug.Print " BegDateTime:" & Format(BegDateTime, "yyyy-mm-dd hh:nn:ss")
End If
BegDate = DateValue(BegDateTime)
Debug.Print " BegDate: " & BegDate
EndDate = DateValue(EndDateTime)
Debug.Print " EndDate: " & EndDate
If BegDate >= EndDate Then
Debug.Print "Adjusted BegDate >= EndDate"
Weekday_Hours = 0
Exit Function
Else ' (BegDate < EndDate)
FirstDay_Hours = DateDiff("h", BegDateTime, BegDate + 1)
Debug.Print " FirstDay_Hours: " & FirstDay_Hours
FirstDay_Minutes = DateDiff("n", BegDateTime, BegDate + 1)
Debug.Print "FirstDay_Minutes: " & FirstDay_Minutes
BegDateTime = DateAdd("d", 1, BegDateTime)
BegDateTime = Format(BegDateTime, "yyyy-mm-dd")
BegDate = DateValue(BegDateTime)
End If
EndTime = TimeValue(EndDateTime)
Debug.Print " BegDateTime: " & Format(BegDateTime, "yyyy-mm-dd hh:nn:ss")
EndDate = DateValue(EndDateTime)
EndTime = TimeValue(EndDateTime)
Debug.Print " EndDateTime: " & EndDate & " " & EndTime
WholeWeeks = DateDiff("w", BegDate, EndDate)
Debug.Print " " & " WholeWeeks: " & WholeWeeks
DateCurrent = DateAdd("ww", WholeWeeks, BegDate)
Debug.Print " DateCurrent: " & DateCurrent
Do While DateCurrent < EndDate
If Weekday(DateCurrent) <> vbSunday And _
Weekday(DateCurrent) <> vbSaturday Then
EndDays = EndDays + 1
Debug.Print " EndDays: " & EndDays
Else
DateCurrent = Format(DateCurrent, "yyyy-mm-dd")
End If
DateCurrent = DateAdd("d", 1, DateCurrent)
Debug.Print " DateCurrent: " & Format(DateCurrent, "yyyy-mm-dd hh:nn:ss")
Loop
Week_Days = WholeWeeks * 5 + EndDays
Debug.Print " Week_Days: " & Week_Days
Debug.Print " Whole day hours: " & Week_Days * 24
BegTime = TimeValue(BegDateTime)
Debug.Print " BegDateTime: " & BegDate & " " & BegTime
LastDay_Hours = DateDiff("h", BegTime, EndTime)
Debug.Print " LastDay_Hours: " & LastDay_Hours
LastDay_Minutes = DateDiff("n", BegTime, EndTime)
Debug.Print " LastDay_Minutes: " & LastDay_Minutes
' Weekday hours
Weekday_Hours = FirstDay_Hours + Week_Days * 24 + LastDay_Hours
Debug.Print " Weekday_Hours: " & Weekday_Hours
Weekday_Hours = (FirstDay_Minutes + LastDay_Minutes) / 60 + Week_Days * 24
Debug.Print " Weekday_Hours: " & Weekday_Hours
End Function
Private Sub Weekday_Hours_test()
Dim BegDateTime As Variant
Dim EndDateTime As Variant
' Whole day hours: 0
' LastDay_Hours: 6
' LastDay_Minutes: 400
' Weekday_Hours: 6 / 7 when using minutes
BegDateTime = "2022-05-05 09:05:19"
EndDateTime = "2022-05-05 15:45:14"
' ?
'BegDateTime = "2022-05-05 15:45:14"
'EndDateTime = "2022-05-05 09:05:19"
' Weekday_Hours: 0
'BegDateTime = "2022-04-30 09:05:19" ' Saturday
'EndDateTime = "2022-05-01 15:45:14" ' Sunday
' FirstDay_Hours: 9
' FirstDay_Minutes: 495
' Whole day hours: 0
' LastDay_Hours: 9
' LastDay_Minutes: 545
' Weekday_Hours: 18 / 17 when using minutes
'BegDateTime = "2022-05-04 15:45:14"
'EndDateTime = "2022-05-05 09:05:19"
' FirstDay_Hours: 15
' FirstDay_Minutes: 895
' Whole day hours: 96
' LastDay_Hours: 15
' LastDay_Minutes: 945
' Weekday_Hours: 126 / 127 when using minutes
'BegDateTime = "2022-05-05 09:05:19"
'EndDateTime = "2022-05-12 15:45:14"
' *** Monday starts at 12AM
' FirstDay_Hours: 24
' FirstDay_Minutes: 1440
' Whole day hours: 48
' LastDay_Hours: 15
' LastDay_Minutes: 945
' Weekday_Hours: 87 / 88 when using minutes)
'BegDateTime = "2022-04-30 09:05:19" ' Saturday
'EndDateTime = "2022-05-05 15:45:14"
' FirstDay_Hours: 15
' FirstDay_Minutes: 895
' Whole day hours: 24
' LastDay_Hours: 15
' LastDay_Minutes: 945
' Weekday_Hours: 54 / 55 when using minutes)
'BegDateTime = "2022-05-05 09:05:19"
'EndDateTime = "2022-05-07 15:45:14" ' Saturday
' *** When time is not entered the default is 12AM ***
' Weekday_Hours: ?
'BegDateTime = "2022-05-05"
'EndDateTime = "2022-05-05"
' One day not two
' FirstDay_Hours: 24
' FirstDay_Minutes: 1440
' Weekday_Hours: 24
'BegDateTime = "2022-05-04"
'EndDateTime = "2022-05-05"
' Three days not four
' FirstDay_Hours: 24
' FirstDay_Minutes: 1440
' Whole day hours: 48
' LastDay_Hours: 0
' LastDay_Minutes: 0
' Weekday_Hours: 72
'BegDateTime = "2022-04-30" ' Saturday
'EndDateTime = "2022-05-05"
If EndDateTime > BegDateTime Then
Debug.Print " Weekday_Hours: " & Weekday_Hours(BegDateTime, EndDateTime)
Else
Debug.Print "?"
End If
End Sub

I can't find the error VB

Image 1
The problem appears when I try to add new bill.
The message says:
conversion from string "4/20/2017" to type 'date' in not valid. at Microsoft.VisualBasic.CompilerServices.conversions.ToDate(string value)
The problem in the line 1054 Dim duedate as String = DateAdd("d", +10, CDate(GetLastDayOfMonth)) and in line 1102 <pre>set_CutoffDates
Image 2
Private Sub set_CutoffDates()
sqlstring = "SELECT " &
" * " &
" FROM " &
" tblBillingProcess "
read_record(sqlstring, "tblBillingProcess")
txtDate.Text = Date.Now.ToShortDateString
' check if first billing get the last month's dates
If ds.Tables("tblBillingProcess").Rows.Count = 0 Then
'Dim datenow As Date = Now.Date
'Dim Prev_Month As String = Month(DateAdd("m", -1, datenow))
'Dim Prev_Year As String = Year(DateAdd("m", -1, datenow))
'Dim firstdayprevmonth As String = Prev_Month & "/01/" & Prev_Year
'Dim GetLastDayOfMonth As String = DateSerial(CInt(Prev_Year), CInt(Prev_Month) + 1, 0)
'txtFrom.Text = firstdayprevmonth
'txtTo.Text = GetLastDayOfMonth
Dim datenow As Date = Now.Date
Dim Prev_Month As String = Month(DateAdd("m", -1, datenow))
Dim Prev_Year As String = Year(DateAdd("m", -1, datenow))
'Dim firstdayprevmonth As String = Prev_Month & "/01/" & Prev_Year
Dim periodFrom As String = Prev_Month & "/21/" & Prev_Year
Dim NOW_Month As String = Month(datenow)
Dim NOW_Year As String = Year(datenow)
Dim firstdayofmonth As String = NOW_Month & "/01/" & NOW_Year
Dim GetLastDayOfMonth As String = NOW_Month & "/20/" & NOW_Year
'Dim duedate As String = DateAdd("d", +10, datenow)
Dim duedate As String = DateAdd("d", +10, GetLastDayOfMonth)
txtFrom.Text = periodFrom
txtTo.Text = GetLastDayOfMonth
txtDueDate.Text = duedate
Else
Dim datenow As Date
'sqlstring = "SELECT " & _
' " MAX(Periodfrom) as Periodfrom " & _
' " FROM " & _
' " tblBillingProcess "
sqlstring = "SELECT " &
" MAX(Periodto) as Periodfrom " &
" FROM " &
" tblBillingProcess "
read_record(sqlstring, "tblBillingProcessdate")
datenow = CDate(ds.Tables("tblBillingProcessdate").Rows(0).Item("Periodfrom").ToString)
'Dim Prev_Month As String = Month(DateAdd("m", -1, datenow))
'Dim Prev_Year As String = Year(DateAdd("m", -1, datenow))
Dim Prev_Month As String = Month(datenow)
Dim Prev_Year As String = Year(datenow)
Dim periodFrom As String = Prev_Month & "/21/" & Prev_Year
Dim NOW_Month As String = Month(DateAdd("m", +1, datenow))
Dim NOW_Year As String = Year(DateAdd("m", -1, datenow))
Dim firstdayofmonth As String = NOW_Month & "/01/" & NOW_Year
Dim GetLastDayOfMonth As String = NOW_Month & "/20/" & NOW_Year
Dim duedate As String = DateAdd("d", +10, CDate(GetLastDayOfMonth))
txtFrom.Text = periodFrom
txtTo.Text = GetLastDayOfMonth
txtDueDate.Text = duedate
End If
End Sub
Private Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
Try
If btnadd.Text = "&Add" Then
If Get_PendingPresentReading() = True Then
MsgBox("You must complete all present reading before creating new billing process ",
MsgBoxStyle.Exclamation, Text)
Exit Sub
End If
reset_controls()
dgProcess.Rows.Clear()
txtBillingNo.Text = String.Empty
txtFrom.Text = String.Empty
txtTo.Text = String.Empty
txtDueDate.Text = String.Empty
txtDate.Text = String.Empty
generate_billingNO()
load_data()
set_CutoffDates()
btnadd.Text = "&Save"
btnedit.Enabled = False
btncancel.Enabled = True
btnMain.Enabled = False
btnFind.Enabled = False
Else
If MsgBox("Are you sure to save this transaction ",
MsgBoxStyle.Question + MsgBoxStyle.YesNo, Me.Text) = MsgBoxResult.No Then
Exit Sub
End If
The last day of the month, here is the calculation:
DateTime now = DateTime.Today;
var lastDate = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month);
then display it. It will use your default culture.
txtDueDate.Text = lastDate.ToShortDateString();
If you want MM/dd/yyyy regardless of whether its an en-US computer, use this:
txtDueDate.Text = lastDate.ToString("MM/dd/yyyy");

Calculate the working hour

I find it difficult to get the actual working hour and minute based on the Pay Amount and Pay rate per hour.
Example : working duration = (wages / pay per hour)
The following is my code. Please help.
strActDuration = CStr(Math.Round((dblActual / dblPayAmount), 1))
Dim parts As String() = strActDuration.Split("."c)
Dim strhour As Integer = 0
Dim strminutes As Integer = 0
If parts.Length = 1 Then
strhour = Integer.Parse(parts(0))
strminutes = 0
ElseIf parts.Length = 2 Then
strhour = Integer.Parse(parts(0))
strminutes = Integer.Parse(parts(1))
'strroundminutes = CInt(Math.Round(strminutes, 3))
'strroundminutes = CInt(Math.Truncate(strminutes / 10))
End If
strCombineDuration = strhour & "Hr " & strminutes & "Min"
Keep the duration as a number (double) and work in minutes; convert to Hours/Minutes using .NET formats:
dblMinutes = Math.Round(60*dblActual/dblPayAmount, 1)
Dim ts as TimeSpan = New Timespan(0, dblMinutes, 0)
strDuration = Format(ts.Hours, "0") & " Hr " & Format(ts.Minutes, "0")) & " Min"

Issue with Counter and a Date

I am havving a little issue with a date like the 1AM bellow but if I use a date like this it works fine "MAR 28, 2014 3PM" for some reason I get a "-17:49" while using the 1AM.. why is this?
Dim myTime As String = "MAR 26, 2014 1AM"
Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss dddd, dd MMMM yyyy")
Dim date2 As DateTime = Convert.ToDateTime(myTime)
Dim ts As New TimeSpan
ts = date2 - date1
If ts.Days > 0 Then
TextBox.Text = ts.Days & ":" & ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds
ElseIf ts.Hours > 0 Then
TextBox.Text = ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds
Else
TextBox.Text = ts.Minutes & ":" & ts.Seconds
If ts.Minutes = 2 And ts.Seconds < 30 Then
doStop = True 'we are 2:30 minutes away from closing
End If
If ts.Minutes < 15 Then
'TextBox.text = "Ending Soon"
End If
End If
You are converting the current date/time to string, then auto-converting it back to a date value immediately on that second line.
Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss dddd, dd MMMM yyyy")
^^^^^^^^ this converts date to string
^^^^^^^^ this converts it back due to implicit casting
But since you are using hh as the format, you get only the 12-hour value of the date, so some information about the date value is lost.
For example, it is currently 12:34 AM EST where I am testing the code, but if I dump out the contents of date1 immediately after that second line, it has already become 12 hours later!
3/26/2014 12:34:41 PM
When you want to work with date values and do math on them, leave them as date values. Don't convert to string until you're ready to display something to the user. So just use:
Dim date1 as DateTime = System.DateTime.Now

Setup a count down based on a time

I would like to know how to setup a count down based on a time.. for example its 6pm and there is a time like 11PM, how would I have a countdown that I click a button no timer, that shows me you have 5 hours left etc.
Dim myTime As String = "7AM"
Dim date1 As DateTime = Date.Now
Dim date2 As DateTime = Convert.ToDateTime(Date.Now.Date.ToLongDateString & " " & myTime)
Dim ts As New TimeSpan
ts = date2 - date1
TextBox.Text = ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds
If I got it right, do you want to get the remaining time by clicking a button? which also means that you have to click it everytime for it to update?
Dim myTime As String = "7AM"
Dim date1 As DateTime = System.DateTime.Now.ToString("hh:mm:ss tt, dd MMMM yyyy")
Dim date2 As DateTime = Convert.ToDateTime(Date.Now.Date.ToLongDateString & " " & myTime)
Dim ts As New TimeSpan
If date2 < date1 Then
date2 = date2.AddDays(1)
End If
ts = date2 - date1
MsgBox(ts.Hours & ":" & ts.Minutes & ":" & ts.Seconds)
That would return a time with 16:30:00 with hh:mm:ss format.