paystub problem, datediff (vb.net)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • double kanon
    New Member
    • Nov 2008
    • 8

    paystub problem, datediff (vb.net)

    hi i am having trouble with this paystub program i am working on. so the console is supposed to ask for the start time and end time then calculate the pay with the rate given.

    How can i enter the start and end times as time and not a double.
    i dimmed them as date but then i dont know how to find the time difference between the two times. am i supposed to use datediff? and if so how does datediff work cause i cant seem to get datediff to work.

    thanks a lot
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    How are you reading in the date/times?
    They probably come in as a string?
    Use it to create/populate DateTime objects.
    Then use operators on them to produce a TimeSpan object.
    you should be able to do what you need with a TimeSpan object

    Comment

    • double kanon
      New Member
      • Nov 2008
      • 8

      #3
      im only using times like 10:35 am till 4:30 pm, then i want a function that would be able to find the difference between the two times in hours. does timespan do this because i have never used it before.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Yup. Create your DateTime objects and then subtract one from the other and cast it to a TimeSpan object.

        Comment

        • double kanon
          New Member
          • Nov 2008
          • 8

          #5
          would you by anychance have an example of this because i have never worked with datetime or timespan before.

          thanks

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            It's in C#, but conceptually it will be the same
            [code=c#]
            DateTime Start = new DateTime(2008, 11, 25, 12, 20, 00);
            DateTime End = DateTime.Now;
            TimeSpan t = (End - Start);
            double hours = t.TotalHours;//The whole time expressed as whole and fractional hours
            [/code]

            So if you used a start time of 2:30pm and an endtime of 4:00pm (instead of my example times), you would get 1.5 as the value for hours

            Comment

            • double kanon
              New Member
              • Nov 2008
              • 8

              #7
              ok well i tried using wat you put but i still cant figure it out. so then i tryed this out:

              Code:
              Public Sub Main()   
                     Dim start as date
              	Dim finish as date
              	Dim hours as double
              	Console.writeline("Enter employee start time")
                      start = CDate(console.readline())
                      Console.writeline("Enter employee end time")
                      finish = CDate(console.readline())
                      hours = datediff("h", start, finish)
                      console.writeline("{0}", start)
                      console.writeline("{0}", finish)
                      console.writeline("{0}", hours)
                  End Sub
              but it wouldnt work either
              Last edited by Curtis Rutland; Nov 25 '08, 06:46 PM. Reason: added code tags

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Let me attempt to fix a few things to make them use .NET and not outdated VB
                [code=vbnet]
                Public Sub Main()
                Dim start as DateTime
                Dim finish as DateTime
                Dim hours as double
                Console.writeli ne("Enter employee start time")
                start = DateTime.Parse( console.readlin e())
                Console.writeli ne("Enter employee end time")
                finish = DateTime.Parse( console.readlin e())
                hours = ((TimeSpan)(fin ish-start)).TotalHo urs
                console.writeli ne("{0}", start)
                console.writeli ne("{0}", finish)
                console.writeli ne("{0}", hours)
                End Sub
                [/code]

                What made your code not work? Was an exception thrown?

                Comment

                • double kanon
                  New Member
                  • Nov 2008
                  • 8

                  #9
                  ok so i tryed this
                  Code:
                  Module time_difference                 
                  	Public Sub Main()   
                  		Dim start as DateTime
                  		Dim finish as DateTime
                  		Dim hours as double
                  		Console.writeline("Enter employee start time")
                  		start = DateTime.Parse(console.readline()) 
                                 Console.writeline("Enter employee end time")
                  		finish = DateTime.Parse(console.readline())         
                  		hours = ((TimeSpan)(finish-start)).TotalHours
                                 console.writeline("{0}", start)
                                 console.writeline("{0}", finish)
                                console.writeline("{0}", hours)
                      	End Sub
                  End Module
                  and i get
                  C:\MSCI 121\timeconvers ion2.vb(15) : error BC30108: 'TimeSpan' is a type and can not be used as an expression.

                  hours = ((TimeSpan)(fin ish-start)).TotalHo urs
                  ~~~~~~~~

                  Comment

                  • rpicilli
                    New Member
                    • Aug 2008
                    • 77

                    #10
                    Hi there.

                    Try this code. After understand what it does, modify to your need

                    Code:
                            Dim dInit As New DateTime
                            Dim dEnd As New DateTime
                            Dim hOurs As New TimeSpan
                    
                            dInit = CType("11/11/2007 10:00", Date)
                            dEnd = CType("11/11/2007 11:38", Date)
                    
                            hOurs = dEnd - dInit
                    
                            MessageBox.Show(hOurs.ToString)

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      (TimeSpan)() is C#'s way of type casting. I think you use Convert or CTYPE or some other wierd thing in VB.

                      Comment

                      • double kanon
                        New Member
                        • Nov 2008
                        • 8

                        #12
                        hey guys thanks a lot.. so now i got something that works and this is the code for it
                        Code:
                        	Public Sub Main()   
                        		Dim start as DateTime
                        		Dim finish as DateTime
                        		Dim hours as Timespan
                        		Console.writeline("Enter employee start time")
                        		start = DateTime.Parse(console.readline()) 
                                Console.writeline("Enter employee end time")
                        		finish = DateTime.Parse(console.readline())         
                        		hours = finish - start
                                console.writeline("{0}", start)
                                console.writeline("{0}", finish)
                                console.writeline("{0}", hours)
                            End Sub
                        but when i get my output it looks like this:
                        Enter employee start time
                        10:35
                        Enter employee end time
                        11:35
                        11/26/2008 10:35:00 AM
                        11/26/2008 11:35:00 AM
                        01:00:00

                        is there a way of not including the date in the output, so i want only the time.
                        and also how can i convert the time difference in decimal number of hours and not an actual time

                        thanks

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          the .ToString() of DateTime object takes a string where you can say like "hh:mm:ss zz" for like "10:35 pm" (Check MSDN for exact format string as i might have it wrong)

                          Also, when you print out the hours variable, consider printing out only the TotalHours property of it, as it's possible you will have over 24hour time period (maybe not in your application, but its important to make sure you are specific, just in case) and you want to make sure its only expressed in hours

                          Comment

                          • double kanon
                            New Member
                            • Nov 2008
                            • 8

                            #14
                            ok i got the time difference in hours by doing this
                            diff = datediff("n", start, finish) ---> this gives me number of minutes so i only have to divide by 60
                            timediff = diff/60

                            ok now i have a different problem, i want to read the inputs from a file now with streamreader but the file would look like this

                            name rate starttime1 endtime1 starttime2 endtime2 starttime3 endtime3 ...
                            name rate starttime1 endtime1 starttime2 endtime2 starttime3 endtime3 ...
                            name rate starttime1 endtime1 starttime2 endtime2 starttime3 endtime3 ...
                            ...
                            ...

                            is there anyway of doing this? so it would be different employee's name and rates and the hours they worked that week

                            thanks

                            Comment

                            • rpicilli
                              New Member
                              • Aug 2008
                              • 77

                              #15
                              Did you try the code I've send you?

                              Rpicilli

                              Comment

                              Working...