How to add time to timespan?

qadeer37

Well-known member
Joined
May 1, 2010
Messages
63
Location
Hyderabad,pakistan
Programming Experience
1-3
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim ts As TimeSpan
ts = New TimeSpan(9, 0, 0)
MessageBox.Show(ts.Hours.ToString)

End Sub

Suppose I want to add 2 hours to this timespan what would be the method for it + code .
 
Timespans can be added using the + operator.
VB.NET:
Dim sum = ts + TimeSpan.FromHours(2)
 
A couple different ways you can write it depending on what makes more sense to you. The first is easier for me to read though I know several people who use the 2nd.

VB.NET:
        Dim ts As New TimeSpan(0, 9, 0, 0, 0)
        MessageBox.Show(ts.Hours.ToString)
        'Add 2 hours
        ts += New TimeSpan(0, 2, 0, 0, 0)
        MessageBox.Show(ts.Hours.ToString)
        'Add 2 more hours
        ts += TimeSpan.FromHours(2)
        MessageBox.Show(ts.Hours.ToString)CODE]
 
Back
Top