I am using VB.NET. I have a dropdownlist named startTimeDDL. Than I am using loop to enter in time inside that dropdownlist.
' Auto fill "Start Time" for DropDownList
Dim StartTime As DateTime = #12:00:00 AM#
For i As Integer = 0 To 47
StartTimeDDL.Items.Add(StartTime.ToString("hh:mm tt"))
StartTime = DateAdd(DateInterval.Minute, 30, StartTime)
Next
So look below and that will be inside the dropdownList. notie the format is hh:mm am/pm.
12:00 AM
12:30 AM
01:00 AM
01:30 AM
02:00 AM
...
11:30 PM
Problem:
lets say current time is 1:21:01 pm than I want to write code so it select 1:30 pm from dropdownlist. Now les take another example. les say current time is 12:00:00 AM than I was to select 12:30 AM from dropdownlist. les take one last example. les say current time is 2:10:12 AM than I want to select 2:30 AM from the dropdownlist.
here is the code I wrote so far. problem with it is that I am only selecting the current time. now can I modfily to do what I want?
Dim dDate As DateTime = DateTime.Now.ToString("hh:mm tt")
Dim temp As String = dDate
StartTimeDDL.Items.FindByValue(temp).Selected = True
Round up if the minute value exceeds 30, round down if it's lower.
Here is an example implementation, you'll need to decide what to do with the "exactly 30 minutes past the hour" edge case. In my code it'll round up for that too.
Private Function RoundDateToHalfHours() As Date
Dim current As DateTime = DateTime.Now
Dim ts As TimeSpan
If current.Minute >= 30 Then
ts = New TimeSpan(current.Hour + 1, 0, 0)
Else
ts = New TimeSpan(current.Hour, 30, 0)
End If
Return current.Date.AddTicks(ts.Ticks)
End Function
Usage:
Dim roundedDate As DateTime = RoundDateToHalfHours()
StartTimeDDL.Items.FindByValue(roundedDate.ToString("hh:mm tt")).Selected = True
You are playing fast and loose with DateTime when you should be using TimeSpan, some caution required. The general way to round up an integral number to an arbitrary interval is
roundedUp = interval * ((number + interval - 1) \ interval)
Which you can readily use on DateTime as well by converting it to ticks, a one-liner
Public Function RoundupDate(dt As DateTime, interval As TimeSpan) As DateTime
Return New DateTime(interval.Ticks * ((dt.Ticks + interval.Ticks - 1) \ interval.Ticks))
End Function
Sample usage:
Dim example = #2:10:12 AM#
Dim rounded = RoundupDate(example, TimeSpan.FromMinutes(30))
Wasn't sure what you meant by a 'dropdownList'. For this example I used a ComboBox.
Dim StartTime As DateTime = #12:00:00 AM#
'load combo box
Do
StartTimeDDL.Items.Add(StartTime.ToString("hh:mm tt"))
StartTime = StartTime.AddMinutes(30)
Loop While StartTime.TimeOfDay.TotalDays > 0
Dim selectTime As DateTime = #2:10:12 PM# 'TEST find this <<<<<<<<<<<<<<<<<<<
'round time to 30 minutes
Dim numSecs As Integer = (CInt(selectTime.TimeOfDay.TotalSeconds) \ 1800) * 1800
'the OP said 'les say current time is 12:00:00 AM than I was to select 12:30 AM"
'so....
numSecs += 1800 'round up 30 minutes ????????
'create 'find'
Dim ts As New TimeSpan(0, 0, numSecs)
Dim findDate As New DateTime(ts.Ticks)
StartTimeDDL.SelectedIndex = StartTimeDDL.FindStringExact(findDate.ToString("hh:mm tt"))
Related
How do I check if my current time is between 06:00:00 AM and 11:00:00 AM?
Is it something like this?
If Now.ToString("hh:mm:ss tt") >= "06:00:00 AM" and Now.ToString <= "11:00:00 AM" Then
'Do something
End If
I'm sorry. I'm still learning.
I feel like there ought to be an easier way to do this, but if there is, I can't think of it. Here's the easiest/safest way I came up with to do it:
Dim curr As Date = Date.Now
Dim startTime As New Date(curr.Year, curr.Month, curr.Day, 6, 0, 0)
Dim endTime As New Date(curr.Year, curr.Month, curr.Day, 11, 0, 0)
If (curr >= startTime) And (curr <= endTime) Then
' Do something
End If
Good. Here is an updated version. Convert 24hour clock time to an integer then do logic on Integers
Dim timeValue As Integer = Now.ToString("HHmm")
If timeValue >= 600 and timeValue <= 1100 Then
' do it now
End If
You're very close. I would do it with 24 hour clock to avoid ambiguity:
Dim timeNow As String = Now.ToString("HH:mm:ss")
If timeNow >= "06:00:00" And timeNow <= "11:00:00" Then
'Do something`
End If
I am trying to subtract hours and minutes from a DateTime variable and I have found other posts that show that you should be able to use the .AddHours(-3) in order to achieve this but it is not working for me. I am grabbing the datetime from a DateTimePicker control in vb.net. say the time is 10:00 AM, I want to subtract 3 hours from this to make it 7:00 AM. My hours variable evaluates to -3 but even when I just literally put the number -3 inside the .AddHours it still does not subtract the time. Heres the code
Dim ApptTime As DateTime = Convert.ToDateTime(DateTimePicker2.Value)
Dim travelTime As String = Label60.Text
Dim newtime As Double
Dim split() As String = travelTime.Split(" ")
If split.Length = 2 Then
Dim Minutes As String = split(0).Replace("mins", "")
Else
Dim Hours As Double = split(0).Replace("Hours", "")
Dim Minutes As Double = split(2).Replace("mins", "")
Hours = -Hours
Minutes = -Minutes
ApptTime.AddHours(Hours)
ApptTime.AddMinutes(Minutes)
Label62.Text = (ApptTime.ToString)
It's simple error ...
Dim ApptTime As DateTime = Now
'ApptTime.Subtract(New TimeSpan(1, 60, 60)) 'won't work
ApptTime = ApptTime.Subtract(New TimeSpan(1, 60, 60)) '1h , 60m , 60s
Try this:
Dim NowMinusThreeHours = DateAdd(DateInterval.Hour, -3, Now)
my input is date.
But, i'm stuck on how to retrieve date time in every second.
I need to put the each second date time in the 2d array.so my array(0,0) should equal to 2/10/2014 00:00:00 AM and array(86399,0) is equal to 2/10/2014 23:59:59 PM.
i tried do looping as per below code:
Dim twoDarray(86399, 1) As String
Dim dtInput As Date
dtInput= #2/10/2014#
For i=0 to 86399
twoDarray(i, 0) = dtInput
dtInput = dtInput +second 'i know this not right
Next
I just don't know how to increase date time every second in right way.
Please help.
Have you thought about something along the lines of
Using a Datetime (MSDN Datetime)
dtInput= new DateTime(2014,10,2)
For i=0 to 86399
twoDarray(i, 0) = dtInput
dtInput = dtInput.AddSeconds(1)
Next
Or
dtInput= new DateTime(2014,10,2)
For i=0 to 86399
twoDarray(i, 0) = dtInput.AddSeconds(i+1)
Next
You can try following method also
Dim dtFrom As New DateTime(2014, 10, 2, 0, 0, 0)
Dim dtTo As New DateTime(2014, 10, 2, 23, 59, 59)
Dim iFirstDim As Integer = (dtTo - dtFrom).TotalSeconds
Dim iSecondDim As Integer = 10
Dim arrTime(iFirstDim, iSecondDim) As String
Dim i As Integer = 0
Do While (dtTo > dtFrom)
arrTime(i, 0) = dtFrom.ToString("d/MM/yyyy HH:mm:ss")
dtFrom = dtFrom.AddSeconds(1)
i += 1
Loop
HOW TO USE IT?
Dim dtResult As DateTime
If DateTime.TryParseExact(arrTime(150, 0), "d/MM/yyyy HH:mm:ss", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dtResult) Then
MsgBox(dtResult.ToString("yyyy-MM-dd HH:mm:ss"))
End If
i try loop while with datetime each on weekly in VB.NET 2008.
This Code
Private Sub Button1_Click()....
'Select DateTime
Dim strDate As Date = dateTimePicker.Value.ToString("yyyy-MM-dd")
'one week (+7)
Dim strDateWeek As String = DateAdd("d", +7, dateTimePicker.Value.ToString("yyyy-MM-dd"))
'DateCurrent
Dim strDateNow As String = DateAdd("d", 0, Now.ToLongDateString())
'While strDate < strDateNow
'ListBox1.Items.Add(strDateWeek)
'End While
ListBox1.Items.Add(strDateWeek)
End Sub
Example
I select on datetimepicker at "04/02/2013"
Output now: 11/02/2013
But I need Output each on weekly
11/02/2013
18/02/2013
25/02/2013 >>> To Current Week
I try loop While, But don't work.
Thanks you for your time. :)
You could do a while loop until the datetime is greater than today?
You want to use DateTime rather than Date, so you can compare to a DateTime.Now
You want to set your actual DatePicker value to a variable, else it will always be the same and you will just get an infinite loop.
Dim datePickerValue As DateTime = DateTimePicker.Value
Dim strDate As Date = DateTimePicker.Value.ToString("yyyy-MM-dd")
Dim strDateWeek As String
Dim strDateNow As String = DateAdd("d", 0, Now.ToLongDateString())
While datePickerValue < DateTime.Now()
strDateWeek = DateAdd("d", +7, datePickerValue.ToString("yyyy-MM-dd"))
datePickerValue = DateAdd("d", +7, datePickerValue.ToString("yyyy-MM-dd"))
ListBox1.Items.Add(strDateWeek)
End While
Just done it on my VS using your naming conventions and this works fine for me
It's been a long time since I didn't have used VB, but maybe I can help?
In your code, using while could be a wrong choice perhaps you could use a for with a break instead.
for I = 1 to 10
Dim strDateWeek As String = DateAdd("d", +7 * i, dateTimePicker.Value.ToString("yyyy-MM-dd"))
.
.
.
or
while(...)
I += 1
Dim strDateWeek As String = DateAdd("d", +7 * i, dateTimePicker.Value.ToString("yyyy-MM-dd"))
Hope that helps.
Try this:
Dim dtAux As Date = dateTimePicker.Value
Dim dtEnd As Date = Date.Today.AddDays(7 - dt.DayOfWeek)
While dtAux <= dtEnd
ListBox1.Items.Add(dtAux.ToString("yyyy-MM-dd"))
dtAux = dtAux.AddDays(7)
End While
The date dtEnd is the last day of the current week, if you want the loop to stop on the current date simply change the while condition to:
While dtAux <= Date.Today
So let's say I have 1400, I want to convert it into 2:00PM
I tried the following:
Dim convertedTime As String = DateTime.ParseExact(theTime,"HHmm", Nothing)
And it would give me this:
6/12/2012 02:00:00 PM
I do not want the date part, neither do I need the seconds. All I need is 2:00PM
How could I achieve this? Thanks!
The ParseExact method returns a DateTime value, not a string. If you assign it to a string variable you will be converting it automatically, which uses the standard formatting.
If you want it in a specific format, then format the DateTime value as a string:
Dim d As DateTime = DateTime.ParseExact(theTime,"HHmm", Nothing);
Dim convertedTime As String = d.ToString("hh:mm tt")
Label1.Text = Format(Now, "hh:mm"): Label1's text= 10:26 (or whatever the time is)
Label1.Text = Format(Now, "hh:mm tt"): Label's text = 10:26 PM
Label1.Text = Format(Now, "dddd dd, MMMM, YYYY"): Label1's text = Thursday 21, August, 2014 (or whatever the date is)
Label1.Text = Now.ToShortTimeString.ToString() (10:26 PM)
Label1.Text = Now.ToLongTimeString.ToString() (10:26:30 PM)
Dim theTime = New Date(2012, 6, 12, 14, 0, 0)
Dim formatted = theTime.ToString("h:mm tt", Globalization.CultureInfo.InvariantCulture)
Custom Date and Time Format Strings
There are two ways to achieve this.
Option 1 (using standard date and time format strings):
Dim theTime As DateTime = new DateTime(2008, 4, 10, 6, 30, 0)
Dim convertedTime As String =
theTime.ToString("t", CultureInfo.CreateSpecificCulture("en-us"))
Option 2 (using custom date and time format strings):
Dim theTime As DateTime = new DateTime(2008, 4, 10, 6, 30, 0)
Dim convertedTime As String = theTime.ToString("hh:mm tt")
In both cases convertedTime will be 6:30 AM
Try This One...
Dim TimeNow As String
TimeNow = TimeOfDay.ToString("h:mm:ss tt")
You Can Use String.Format() Function Without ParseExtract() Function
VB.NET:-
Dim time As String = String.Format("{0:hh}:{0:mm} {0:tt}", Date.Now) '12:00 PM'
Dim time_1 As String = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", Date.Now) '12:00:25 PM'
Dim time_2 As String = String.Format("{0:mm}:{0:ss}.{0:fff}", Date.Now) '00:25.986 - Mostly Used In Stopwatches'
CSharp (C#):-
String time = String.Format("{0:hh}:{0:mm} {0:tt}", DateTime.Now); // 12:00 PM
String time_1 = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", DateTime.Now); // 12:00:25 PM
String time_2 = String.Format("{0:mm}:{0:ss}.{0:fff}", DateTime.Now); // 00:25.986 - Mostly Used In Stopwatches
Above Codes Are Examples, You Can Use Above Codes To Experiment/Observe/Make Applications With It
stackoverflowuser:
You Can Use String.Format() Function Without ParseExtract() Function
VB.NET:-
Dim time As String = String.Format("{0:hh}:{0:mm} {0:tt}", Date.Now) '12:00 PM'
Dim time_1 As String = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", Date.Now) '12:00:25 PM'
Dim time_2 As String = String.Format("{0:mm}:{0:ss}.{0:fff}", Date.Now) '00:25.986 - Mostly Used In Stopwatches'
CSharp (C#):-
String time = String.Format("{0:hh}:{0:mm} {0:tt}", DateTime.Now); // 12:00 PM
String time_1 = String.Format("{0:hh}:{0:mm}:{0:ss} {0:tt}", DateTime.Now); // 12:00:25 PM
String time_2 = String.Format("{0:mm}:{0:ss}.{0:fff}", DateTime.Now); // 00:25.986 - Mostly Used In Stopwatches
Above Codes Are Examples, You Can Use Above Codes To Experiment/Observe/Make Applications With It
You Can Use Also In:
VB.NET:-
Label1.Text = String.Format("{0:hh}:{0:mm} {0:tt}", Date.Now) 'Display: 12:00 PM'
CSharp (C#):-
label1.Text = SString.Format("{0:hh}:{0:mm} {0:tt}", DateTime.Now); // Display: 12:00 PM
You Will Get The Formatted Timings In Any Label In Both VB.NET And C#