Datetime add timespan without showing the date VB - vb.net

I am currently running this code, and it displays the date as Output shown below.
Dim started As DateTime = DateTime.Now
Dim date As DateTime = started.Add(diaryItem.Duration)
Output : 23/03/2016 17:00:00
Is it possible to remove the date and only show the time ?

If you only want to see time from datetime then try this:
'show time
Dim OnlyTime As String = DateTime.Now.ToString("hh:mm:ss")
'or
Dim OnlyTime = DateTime.Now.ToShortTimeString
If you only want to see date from datetime then try this:
'show date
dateOnly = myDateTime.Date
'or
dateString = dateOnly.ToShortDateString()
If this is not what you are looking for then put more informations..
DateTime Structure

Related

VB.NET - How do I find difference in dates in MM/DD format?

If I have two dates
Dim date1 As String = 01/05
Dim date2 As String = 01/07
Dim date As Integer
Date = 2
How do I go about making sure that 2 handles month changes and just general dates in this format?
You would parse the strings into DateTime values, then you can subtract them to get a TimeSpan value that is the difference. Use the Days property to get the whole days from the value:
Dim date1 As String = "01/05"
Dim date2 As String = "01/07"
Dim d1 = DateTime.ParseExact(date1, "MM'/'dd", CultureInfo.InvariantCulture)
Dim d2 = DateTime.ParseExact(date2, "MM'/'dd", CultureInfo.InvariantCulture)
Dim date As Integer = (d2 - d1).Days

DateTime change date value and set specific value of time in VB.Net

I have value of this for example
Dim start_time As String = 12/30/1899 8:30:00 PM
Then i want to change it into
Dim final_start_time As String = 0000-00-00 20:30:00 ' i want to come up with this value
or another example is
Dim start_time As String = 12/30/1899 10:30:00 AM
to
Dim final_start_time As String = 0000-00-00 10:30:00
Based on your revised Question, here is some code. Revise it as necessary. I used es-ES as culture.. actually have no clue what culture that is... but on the dateformat MSDN page, that seems to generate the format you want...
Dim myOriginalDateTime As DateTime = Now
Dim mynewDateString As String = ""
Dim myFinalDateString As String = ""
Dim culture As New System.Globalization.CultureInfo("es-ES")
mynewDateString = myOriginalDateTime.ToString("G", culture)
Dim arrDateParts() As String = Split(mynewDateString, " ")
myFinalDateString = "0000-00-00 " & arrDateParts(1)
MsgBox(myFinalDateString)
Try this link:
http://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx
And this:
http://msdn.microsoft.com/en-us/library/vstudio/az4se3k1(v=vs.100).aspx
You use the .tostring method, and specify a format.

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

date not formatting correctly on a date that does not exist

I have the following code to convert a date (from a database entry) into the correct format:
Dim blah As Date = "02/29/2001"
MsgBox(Format(blah, "yyyy-MM-dd"))
However, it keeps telling me this:
Conversion from string "02/29/2001" to type 'Date' is not valid.
Now i see that 2/29/2001 does not have a day 29- only 28 for that year. So how would i check for this so it doesn't throw an error when a date in the database table is not entered correctly?
You can use IsDate()
This function returns true if your string or date can be converted to date.
like this
Dim dateAndTime As String
dateAndTime = "March 15, 1981 10:22 AM"
noDate = "Hello"
dateCheck = IsDate(dateAndTime)
dateCheck = IsDate(noDate)
This will return true for the first one and false for the second one
Also, this function works with month of 29, 30 or 31 days like in your case
here's the msdn link
You could use TryParseExact instead, so something like:
Dim enUS As New CultureInfo("en-US")
Dim dateString As String
Dim blah As Date
' Parse date with no style flags.
dateString = "02/29/2001"
If Date.TryParseExact(dateString, "MM/dd/yyyy", enUS, DateTimeStyles.None, dateValue) Then
MsgBox(Format(blah, "yyyy-MM-dd"))
Else
MsgBox(String.Format("'{0}' is not in an acceptable format.", dateString))
End If

how to avoid the changing the date in vb.net

I would like to ask about how to code datetime in CONSOLE vb.net. I already got the format but my problem is after I inputted another data today to the database.
In this image,my data retrieval was error. The ID (1,2,3,4,5,6)should be jan 2. Unfortunately, the output was today after Ive put new data to the database. I was confused on how to code to avoid changing the date.
this is my code:
Dim datetoday As DateTime = DateTime.Now
Dim dateyesterday As DateTime = DateTime.Today
Dim format As String = "yyyy d MMM"
Sub Main()
s = New Sessions
ds = s.GetSession("select CLIENTID, SESSIONCOST from SESSIONS ORDER BY CLIENTID ASC")
Dim startVal = 0
Dim endVal = ds.Tables(0).Rows.Count
For var = startVal To endVal - 1
Console.WriteLine(ds.Tables(0).Rows(var)("CLIENTID").ToString() + " " + ds.Tables(0).Rows(var)("SESSIONCOST").ToString() + " " + datetoday.ToString(format)) 'code for display id and name
Next
Im hoping for you feedback. A sample code would be enough to understand. Thank you
Having the code in your original post is much better - but have you actually read what you've written?
Dim datetoday As DateTime = DateTime.Now
Dim dateyesterday As DateTime = DateTime.Today
These two are going to be pretty much the same thing. If you want yesterday, you would need to subtract a day from Now
Console.WriteLine(ds.Tables(0).Rows(var)("CLIENTID").ToString() + " " + ds.Tables(0).Rows(var)("SESSIONCOST").ToString() + " " + datetoday.ToString(format)) 'code for display id and name
Also - it doesn't appear that you are retrieving (or even storing?) the date in your database. If you are simply printing out the result of DateTime.Now then the date printed next to each row will always be the current date, and not necessarily when the object was created.
Try to use UTC dates (DateTime.UtcNow) in both your app and in your datastore. Only format it with the timezone adjusted date when displaying to a user.