Results 1 to 3 of 3

Thread: Time related Problem

  1. #1
    Zaffryn is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2013
    Posts
    4
    Reputation
    0

    Time related Problem

    Good day all!

    I am currently working on a little timer app where you click start button that will display the time of day when you start your work. Then the stop button will do the same but display the time when you finish your task. My msgbox for now displays a few infos including the difference(WorkTime) between the TimeStop and the TimeStart. What i am trying to do is I need to have the WorkTime to be rounded to the 15 mins. Lets say anything from 0mins to 15 mins will set Worktime to 15, 15 to 30 will set to 30 etc.. 2hours will be something like 120. here is my code :

    Code:
    Public Class Form1
    
        Dim TimeStart As Date
        Dim TimeStop As Date
        Dim Section As String
        Dim Conseiller As String
        Dim Informations As String
        Dim TotalTime As TimeSpan
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            btnStop.Enabled = False
            btnSave.Enabled = False
    
        End Sub
    
        Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
            'Set l'heure a laquelle on debute le travail
            TimeStart = TimeOfDay
            lblTimeStart.Text = TimeStart
    
            btnStop.Enabled = True
            btnStart.Enabled = False
    
        End Sub
    
        Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
            ' Set l'heure a laquelle on termine le travail
            TimeStop = TimeOfDay
    
            lblTimeStop.Text = TimeStop
    
            btnStop.Enabled = False
            btnSave.Enabled = True
    
    
        End Sub
    
    
        Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    
            Dim TotalTimeString As String
    
            Section = cmbSection.SelectedItem
            Conseiller = txtConseiller.Text
            Informations = txtInformations.Text
    
            TotalTime = TimeStop - TimeStart
    
            TotalTimeString = TotalTime.ToString
    
            MsgBox(Section & "," & Conseiller & "," & Informations & "," & TotalTimeString)
    
    
    
        End Sub
    End Class
    Everything I have in my code is working so far. Anyone got any Idea how I can get my problem resolved??

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    12,517
    Reputation
    1609
    E.g.
    Dim startTime As Date
    Dim endTime As Date

    '...

    Dim difference As TimeSpan = endTime - startTime
    Dim minutes As Double = difference.TotalMinutes

    Dim truncatedMinutes As Integer = (CInt(Math.Truncate(minutes)) \ 15) * 15
    Dim roundedMinutes As Integer = CInt(Math.Round(minutes / 15)) * 15

    Dim truncatedDifference As TimeSpan = TimeSpan.FromMinutes(truncatedMinutes)
    Dim roundedDifference As TimeSpan = TimeSpan.FromMinutes(roundedMinutes)

    MessageBox.Show(String.Format("{0:h\:mm} = {1} minutes", truncatedDifference, truncatedMinutes), "Truncated")
    MessageBox.Show(String.Format("{0:h\:mm} = {1} minutes", roundedDifference, roundedMinutes), "Rounded")

  3. #3
    Zaffryn is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Oct 2013
    Posts
    4
    Reputation
    0
    Thank you very much it worked.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking