How to set only time part of a DateTime - vb.net

I need to set datetime variable but it's time part must be 3:00:00 AM.
For example if i call getdate() now i'll get 1/20/2014 3:52:03 AM. I need to set it to 1/20/2014 3:00:00 AM.
Does anybody have a good snippet for that or any ideas how to do it right? would really appreciate some help.

The constructor for the DateTime allows you to specify hours, minutes and seconds as well as the usual day month and year.
Dim now As DateTime = DateTime.Now
Dim myDate = New DateTime(now.Year, now.Month, now.Day, 3, 0, 0, 0)

There are a lot of ways to do this - reading the documentation might give you more ideas
Dim someDate As DateTime = DateTime.Now()
Dim threeAmDate As DateTime = New DateTime(someDate.Year, _
someDate.Month, _
someDate.Day, _
3, _ 'hours
0, _ 'minutes
0, _ 'seconds
0) 'milliseconds
Console.WriteLine(threeAmDate)

try this
Label1.Text = DateAndTime.Today & " " & TimeOfDay
you can put this in a label or anything and you can change time of day to "3:00:00"

This one is old... But I came across having the same question.
Dim dt As New DateTime(_DateTo.Ticks + _TimeTo.Ticks)
--> Both '_DateTo' and/or '_TimeTo' could be Timespan or Datetime objects.

Related

Subtracting Hours and Minutes from DateTime not working

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)

How To Declare / Store "12:00:00 AM" in a DateTime Variable? (Through military time)

I have this code:
Dim twelve_AMToday As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 24, 0, 0)
But it says "Hour, Minute, and Second parameters describe an un-representable DateTime."
Of course 23, 59, 59 is read as 11:59:59 PM. I've tried 0, 0, 0 but then gives me the date only.
Thanks for any help!
You get it when you use Today
Dim twelve_AMToday As DateTime = Today
If you want to add days put this code under it.
twelve_AMToday = twelve_AMToday.AddYears(0).AddMonths(0).AddDays(1)
Try this
Dim twelve_AMToday As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0)
Dim s As String = twelve_AMToday.ToLongDateString & " " & twelve_AMToday.ToLongTimeString
What you see in the debugger will be the same for the datetime, but once the datetime is converted to a string you will see that it does have the time you want. If the time is all zeroes the debugger doesn't show the time for datetimes, but it is there.

How can i retrieve date time available per second in one day?

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

How use loops by datetime each on weekly in vb.net

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

Best way to convert total minutes into HH:mm format?

I get a return value from a web service in minutes, for example 538 minutes. I need to break this down in hours and minutes. What is the fastest way, in .net code and also VB6 code (two apps use the service) to convert this from minutes to HH:mm?
Thanks
This code should work both in .NET and VB6:
Dim hours As Integer = 538 \ 60
Dim minutes As Integer = 538 - (hours * 60)
Dim timeElapsed As String = CType(hours, String) & ":" & CType(minutes, String)
label1.Text = timeElapsed
In .NET exclusively, you should be able to do the following (which requires to be tested):
Dim timeElapsed As DateTime = New DateTime(1, 1, 1, 0, 538, 0)
label1.Text = timeElapsed.ToString("HH:mm")
I hope this helps!
In .Net you have a TimeSpan class so you can do the following
Dim t As New TimeSpan(0, 538, 0)
'Then you have the 2 properties
t.Hours
t.Minutes
In VB6 you could just use Format(538/1440.0, "hh:mm")
VB6 Date values can be treated as a number of days, and there's 1440 minutes in a day. So 538/1440 is the number of days in your period, and then you can use Format
Take modulus 60, then integer-divide by 60.
In VB, integer division uses \.
If you need to perform operations with other DateTime objects it might be useful to use a TimeSpan object instead, e.g.
Dim oTS As New TimeSpan(0, 538, 0)
MessageBox.Show(Format(oTS.Hours, "00") & ":" & Format(oTS.Minutes, "00"))
Dim startime As DateTime = Date.Now
Dim newtime As DateTime = startime + oTS
MessageBox.Show(newtime.ToString("HH:mm"))
If not then Matthew's suggestion of using Integer division '\' and modulo 'Mod' will work very well.
if you need strings then:
dt.ToString("HH:mm");
(info)