Results 1 to 7 of 7

Thread: [RESOLVED] how to get only time from datetime part

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    88

    Resolved [RESOLVED] how to get only time from datetime part

    hi..i have a table where in date column i have datetime datatype..i want to get only time in textbox...but i m getting both date and time by this below code..help anyone...thanks
    i m using vs 2005...sql 2005


    Code:
    Try
                    cn = New SqlConnection("server=localhost;database=attendance1;uid=sa;password=;")
    
                  
                    cn.Open()
                    cmd = New SqlCommand("select * from shift where shift_name='" & ComboBox1.Text & "' ", cn)
                    dr = cmd.ExecuteReader
                    While dr.Read()
                        TextBox1.Text = dr(1) 
                       
                    End While
                Catch
                End Try
                dr.Close()
                cn.Close()

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to get only time from datetime part

    The DateTime structure has methods to return a string representation. To get the time only you can call ToLongTimeString or ToShortTimeString. You can also call ToString and specify any standard or custom format string you want to get the date and/or time in any format. MSDN provides all the information you need on date and time format string. As an example, ToString("t") is equivalent to ToShortTimeString() and ToString("T") is equivalent to ToLongTimeString().
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    88

    Re: how to get only time from datetime part

    i got blank by doing that....nothing appeared..do you have any code?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to get only time from datetime part

    Quote Originally Posted by anirai View Post
    do you have any code?
    Do you? If you don't show us what you did then we can't tell you what you did wrong.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    88

    Re: how to get only time from datetime part

    i posted my code into the first reply above...

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to get only time from datetime part

    That was your original code. I've told you what you need to do to achieve your goal and you said that you tried it and it didn't work. You need to show us that new code that you tried so we can tell what's wrong with it. If you change your code then the original is useless to us.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    88

    Re: how to get only time from datetime part

    thanks for your help//at first i used tostring("t")..no result..then i used toshorttimestring then i got only time...wonderfull....

    Code:
    TextBox1.Text = dr(1).toshorttimestring

Posting Permissions

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



Click Here to Expand Forum to Full Width