dcsimg
Results 1 to 8 of 8

Thread: vb.net: calcal 2x time

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2015
    Posts
    85

    vb.net: calcal 2x time

    hello , how to remove last sec with -
    I did try with format time and nothing happened change

    am getting 0ver none 13:00:00-

    my code
    Code:
     Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
    
            Dim d1 As Date = Date.Now.ToString("08:00")
            Dim d2 As Date = Format(DateTimePicker1.Value, "hh:mm")
            TextBox70.Text = (d2 - d1).ToString
    End Sub

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,687

    Re: vb.net: calcal 2x time

    d1 and d2 are dates and when you subtract the two you get a TimeSpan object. You should be formatting the time span using either one of the standard format strings or a custom format string when you set the textbox text value.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    33,132

    Re: vb.net: calcal 2x time

    While that's true, I suspect it still won't do quite what you want. You appear to be trying to ultimately subtract two dates while ignoring the seconds. You've gone through some contortions to do that, and you may need to, but that isn't clear, yet. In both cases, you take perfectly good dates and convert them to strings, then implicitly convert them back to dates. Leave them as dates:
    Code:
    Dim d1 As Date = Date.Now()
    Dim d2 As Date = DateTimePicker1.Value
    When you subtract the two, that is where you can do whatever manipulation is needed...if it is still needed. If you take a look at the options when you type (d2-d1)., you will see that there are quite a few. In particular, take a look at the methods starting with Total. These will give you the difference in a variety of units of time. So, if you chose TotalMinutes, then you'd get a decimal with the minutes and fractions of a minute. At that point, you can round down, which would do about the same as what you were trying to do....though maybe not quite the same. Still, it's probably pretty close. I can think of one scenario where it would flip the sign, I just doubt it applies.
    My usual boring signature: Nothing

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    101,249

    Re: vb.net: calcal 2x time

    Quote Originally Posted by kebo View Post
    d1 and d2 are dates and when you subtract the two you get a TimeSpan object. You should be formatting the time span using either one of the standard format strings or a custom format string when you set the textbox text value.
    Just be aware that the ability to format a TimeSpan that way was added fairly recently; .NET 4.5 I think. If you're using an earlier version then the simplest option is to simply add your TimeSpan to a Date that has no time portion, e.g. Date.MinValue or Date.Today, and then use a date/time format specifier on that, e.g. "HH:mm".

  5. #5
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,628

    Re: vb.net: calcal 2x time

    Hi,

    here a sample with Customformat for the DTP, but why are the seconds a problem for you ?

    Code:
    Public Class Form4
    
        Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            iniControls()
        End Sub
    
        Private Sub iniControls()
            With DateTimePicker1
                .ShowUpDown = True
                .Font = New Font("Arial", 14)
                .SetBounds(10, 10, 80, .Height)
                .Format = DateTimePickerFormat.Custom
                .CustomFormat = "HH:mm"
            End With
            Dim Top As Integer = DateTimePicker1.Bottom + 10
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2015
    Posts
    85

    Re: vb.net: calcal 2x time

    Quote Originally Posted by Shaggy Hiker View Post
    While that's true, I suspect it still won't do quite what you want. You appear to be trying to ultimately subtract two dates while ignoring the seconds. You've gone through some contortions to do that, and you may need to, but that isn't clear, yet. In both cases, you take perfectly good dates and convert them to strings, then implicitly convert them back to dates. Leave them as dates:
    Code:
    Dim d1 As Date = Date.Now()
    Dim d2 As Date = DateTimePicker1.Value
    When you subtract the two, that is where you can do whatever manipulation is needed...if it is still needed. If you take a look at the options when you type (d2-d1)., you will see that there are quite a few. In particular, take a look at the methods starting with Total. These will give you the difference in a variety of units of time. So, if you chose TotalMinutes, then you'd get a decimal with the minutes and fractions of a minute. At that point, you can round down, which would do about the same as what you were trying to do....though maybe not quite the same. Still, it's probably pretty close. I can think of one scenario where it would flip the sign, I just doubt it applies.
    am trying to calcal to datetime for 12h start time from 8:00 am the second time from 10:00 AM ~ 12:00 PM to 2:30 PM, but when I try there is no difference still same thing.
    I just change my code this is good right now but the problem the second I don't need it

    UPDATE12h = free time mean break each month break just 12h they can use it a day 3~4h from start time work 8:00 am or the end from 10:am ~ 2:30 PM
    Code:
      Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
    
            Try
    
                If RadioButton1.Checked = True Then
                    Dim d1 As Date = ("08:00 AM")
                    Dim d2 As Date = Format(DateTimePicker1.Value, "hh:mm tt")
    
                    TextBox70.Text = (d2 - d1).ToString
                  
                Else
                    If RadioButton2.Checked = True Then
                        Dim d1 As Date = ("2:30 PM")
                        Dim d2 As Date = Format(DateTimePicker1.Value, "hh:mm tt")
    
    
                        TextBox70.Text = (d1 - d2).ToString
                     
                    End If
                End If
    
            Catch ex As Exception
    
            End Try
    Attached Images Attached Images  
    Last edited by kankon; Feb 14th, 2019 at 07:30 AM.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2015
    Posts
    85

    Re: vb.net: calcal 2x time

    Quote Originally Posted by ChrisE View Post
    Hi,

    here a sample with Customformat for the DTP, but why are the seconds a problem for you ?

    Code:
    Public Class Form4
    
        Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            iniControls()
        End Sub
    
        Private Sub iniControls()
            With DateTimePicker1
                .ShowUpDown = True
                .Font = New Font("Arial", 14)
                .SetBounds(10, 10, 80, .Height)
                .Format = DateTimePickerFormat.Custom
                .CustomFormat = "HH:mm"
            End With
            Dim Top As Integer = DateTimePicker1.Bottom + 10
        End Sub
    thanks I ll try it right now


    2- update : ur code only Customformat for DateTimePicker1
    the problem with textbox70
    Last edited by kankon; Feb 14th, 2019 at 03:34 AM.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2015
    Posts
    85

    Re: vb.net: calcal 2x time

    probable from sql server record data?
    i did try with
    time7 , date, datetime and nvarchar , same thing

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width