Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 218801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:47:07+00:00 2026-05-11T18:47:07+00:00

I want to display the duration only Hour, Minutes, and Second in data Gridview

  • 0

I want to display the duration only Hour, Minutes, and Second in data Gridview by Subtract TimeCheckOut from TimeCheckIn in ASP.NET using LINQ to SQL

Here is code behind:

Dim db = new MyDataContext
Dim user = from u in db.Employees select IDNumber = u.IDNumber, _
           FirstName = u.firstName, LastName = u.lastName, TimeCheckIn = u.timeCheckIn, _
           TimeCheckOut = u.timeCheckOut, Duration = u.timeCheckIn
Gridview1.DataSource = user  
Gridview1.DataBind()

Code on page:

  <asp:GridView ID="GridView1" runat="server" Width="100%" 
    AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="IDNumber" HeaderText="ID Number" ReadOnly="True" />
        <asp:BoundField DataField="FirstName" HeaderText="First Name" ReadOnly="True"/>
        <asp:BoundField DataField="LastName" HeaderText="Last Name" ReadOnly="True"/>
        <asp:BoundField DataField="TimeCheckIn" HeaderText="Time Check In" ReadOnly="True"/>
        <asp:BoundField DataField="TimeCheckOut" HeaderText="Time Check Out" ReadOnly="True" />
        <asp:TemplateField HeaderText="Duration">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# FieldDisplayDuration(Eval("Duration")) %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Here is my helper for FieldDisplayDuration:

Protected Function FieldDisplayDuration(ByVal Duration As DateTime) As String
    Dim rtn As String = "DefaultValue"
    Dim dif As TimeSpan = DateTime.Now.Subtract(Duration)
        rtn = dif.Hours & " hours, " & dif.Minutes & " minutes, " & dif.Seconds & " seconds. "
    Return rtn
End Function

In Line 3 in Helper function, Dim dif as TimeSpan = DateTime.Now.Subtract(Duration) which give only the duration of Hour, Minute, and Second from TimeCheckIn until DateTime.Now. However, I want to have the duration from TimeCheckIn until TimeCheckOut only in Hour, Minute, and Second. I know that the FieldDisplayDuration function is totally wrong logic, but I just want you to get my point only, and also it could be the code sample for those who want to calculate the duration of the employee from the hire date. Finally, Let’s get back to TimeSpan by Subtract TimeCheckOut from TimeCheckIn in gridview problem, How can I do that? Please give me some clue.. Thanks you so much…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:47:07+00:00Added an answer on May 11, 2026 at 6:47 pm

    It is possible to just subtract the date and time to get the Timespan so you should be able to use

     Text='<%# FieldDisplayDuration(Eval("TimeCheckIn"), Eval("TimeCheckOut")) %>'
    

    and something like this function:

    Protected Function FieldDisplayDuration(ByVal CheckIn As DateTime, ByVal CheckOut as DateTime) As String    
        Dim rtn As String = "DefaultValue"    
        Dim dif As TimeSpan = CheckOut - CheckIn        
        rtn = dif.Hours & " hours, " & dif.Minutes & " minutes, " & dif.Seconds & " seconds. "    
        Return rtnEnd 
    Function
    

    Though I would probably use a string format on the rturn line and do this…

    rtn = string.Format("{0} hours, {1} minutes, {2} seconds.", dif.Hours, dif.Minutes, difSeconds)
    

    As I think it is easier to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want display data from database in Listbox...Here is my code, It is not
Is there a way to display a number as an hour from a duration
i want display 1 record from colums zodys , I'm programint in C# I
I want to display duration with milliseconds on a web page. So far I
i want my c# program to be able to display the online duration of
I have news application that needs to download data from the url and display
Hi I want to display player position and player duration in simple date format.
I'm currently developing an ASP.NET application which uses a MasterPage and I want to
I have a simple quiz application and I want display a nice timer /
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.