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.
Related
Dim start_date As Date
start_date = #4/25/2019#
sqll = conn.Execute("SELECT sessionid from BPASession where startdatetime = " & start_date & " ")
Above code is working fine
Below code is also working fine when passing a static date time to query
sqll = conn.Execute("SELECT sessionid from BPASession where startdatetime = '2019-04-24 13:06:09.66' ")
Below code is throwing an error
Dim start_date As Date
start_date = "4/25/2019 5:6:7"
start_date = Format(start_date, "yyyy-mm-dd hh:mm:ss")
sqll = conn.Execute("SELECT sessionid from BPASession where startdatetime = " & start_date & " ")
Value in SQL Server database are in below format-
2019-04-25 09:54:04.347
I am writing code in Excel vba editor and connecting with SQL Server.
Try this:
Dim start_date As Date
start_date = "4/25/2019 5:6:7"
sqll = conn.Execute("SELECT sessionid from BPASession where startdatetime = '" & _
Format(start_date, "yyyy-mm-dd hh:mm:ss") & "'")
Here's your problem:
Dim start_date As Date, sDate As String
start_date = "4/25/2019 5:6:7"
Debug.Print start_date '>> 4/25/2019 5:06:07 AM
start_date = Format(start_date, "yyyy-mm-dd hh:mm:ss") '<< assign to Date
Debug.Print start_date '>> 4/25/2019 5:06:07 AM (OOPS)
sDate = Format(start_date, "yyyy-mm-dd hh:mm:ss") '<< assign to String
Debug.Print sDate '>> 2019-04-25 05:06:07 (OK)
I'm trying to convert different date formats into an appropriate SQL DateTime format and am getting errors.
I have a client's machine that is setup with the following date format, "dd-mm-yyyy" (Localization is India)
When using Now() within VB.Net it parses the month and days backwards. I've tried the following code but get conversion errors.
Dim Month As String = Nothing
Dim Year As String = Nothing
Dim Day As String = Nothing
Dim Hour As String = Nothing
Dim Minute As String = Nothing
Dim Second As String = Nothing
Dim strDate As String = Nothing
Dim Date_to_SQL_DateTime As Date
Month = DatePart(DateInterval.Month, Now()).ToString
Year = DatePart(DateInterval.Year, Now())
Day = DatePart(DateInterval.Day, Now())
Hour = DatePart(DateInterval.Hour, Now())
Minute = DatePart(DateInterval.Minute, Now())
Second = DatePart(DateInterval.Second, Now())
strDate = Month & “/” & Day & “/” & Year & “ “ & Hour & “:” & Minute & “:” & Second
Date_to_SQL_DateTime = Date.ParseExact(strDate, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture)
strDate returns
1/24/2017 23:17
Error I'm receiving is
String was not recognized as a valid Datetime
Try below. Need to make sure parameters are of same format as specified in the ParseExact function.
Dim Month As String = Nothing
Dim Year As String = Nothing
Dim Day As String = Nothing
Dim Hour As String = Nothing
Dim Minute As String = Nothing
Dim Second As String = Nothing
Dim strDate As String = Nothing
Dim Date_to_SQL_DateTime As Date
Month = DatePart(DateInterval.Month, Now()).ToString("00")
Year = DatePart(DateInterval.Year, Now()).ToString("0000")
Day = DatePart(DateInterval.Day, Now()).ToString("00")
Hour = DatePart(DateInterval.Hour, Now()).ToString("00")
Minute = DatePart(DateInterval.Minute, Now()).ToString("00")
Second = DatePart(DateInterval.Second, Now()).ToString("00")
strDate = Month & "/" & Day & "/" & Year & " " & Hour & ":" & Minute & ":" & Second
Date_to_SQL_DateTime = DateTime.ParseExact(strDate, "MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture)
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
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
I Got StopWatch , And i want that the stopwatch start to count from 01:00:12,000
my code(not works):
Dim sp As New Stopwatch
Dim lb As New Label
Me.Controls.Add(lb)
lb.Text = ""
sp.Elapsed.Hours.Equals(1)
sp.Elapsed.Minutes.Equals(0)
sp.Elapsed.Seconds.Equals(12)
sp.Elapsed.Milliseconds.Equals(0)
sp.Start()
lb.Text = sp.Elapsed.Hours.ToString & ":" & sp.Elapsed.Minutes.ToString & ":" & sp.Elapsed.Seconds.ToString & "," & sp.Elapsed.Milliseconds.ToString
This is not possible.
There is no way to initialize the Stopwatch class to anything but 0.
Nothing is stopping you from adding 1 hour and 12 seconds to the result though.
lb.Text = (sp.Elapsed.Hours + 1).ToString & ":" & sp.Elapsed.Minutes.ToString _
& ":" & (sp.Elapsed.Seconds + 12).ToString & "," _
& sp.Elapsed.Milliseconds.ToString
The StopWatch doesn't work that way. What you need to do is record the start time in a different variable and then add the Stopwatch's elapsed time to the start time
Dim StartTime AS Date = Date.Now
...
Dim EndTime AS Date = StartTime.AddMilliseconds(StopWatch1.ElapsedMilliseconds)