You must Sign In to post a response.
  • Category: ASP.NET

    How to calculate the total hours for a particular date in asp.net


    Are you looking for a way to calculate the total hours for a particular date in asp.net ? then read this thread to know how to calculate it



    SELECT CONVERT(VARCHAR(10),InTime,108 ), CONVERT(VARCHAR(10),outTime,108 ) FROM aten WHERE Id = '3' and date between ' 01/10/2014' and '31/10/2014'


    this is my coding in asp.net



    string constr = ConfigurationManager.AppSettings["s"].ToString();
    using (SqlConnection conn = new SqlConnection(constr))
    {
    using (SqlCommand cmd = new SqlCommand("SELECT CONVERT(VARCHAR(10),InTime,108 ), CONVERT(VARCHAR(10),outTime,108 ) FROM aten WHERE Id = '" + txtid.Text + "' and date between ' " + Session["fromdate"].ToString() + "' and '" + Session["todate"].ToString() + "'", conn))
    {
    conn.Open();
    SqlDataReader sdr = cmd.ExecuteReader();
    TimeSpan i = new TimeSpan(0, 0, 0);
    while (sdr.Read())
    {
    DateTime inTime = Convert.ToDateTime(sdr[0].ToString(), new CultureInfo("en-Gb"));
    DateTime outTime = Convert.ToDateTime(sdr[1].ToString(), new CultureInfo("en-Gb"));
    i += outTime.Subtract(inTime);
    double hours = i.Hours + (i.Days * 24);
    string time = string.Format("{0} : {1}", hours, i.Minutes);
    this.txthourswrk.Text = time;
    }
    conn.Close();
    }
  • #751446
    Hi mam, What you are actually trying to do? You needs to calculate the hours between the given dates? if its your requirement then refer this article for your reference. there they have given date direclt instead of that change to your date variable and make changes in ur code as per ur requirement.

    http://www.dailyfreecode.com/code/find-total-hours-elapsed-given-date-202.aspx

    thanks for reading.Please let us know if you need clarification in this.

    Rajesh B
    To Live More,Learn More

  • #751455
    Hai selvi,
    What is the issues you are having in this. Are you getting any error or unexpected results by the code you are using.
    Please write the detailed description for any issue so that it will be easy for us to understand and provide the better solution.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #751458
    am getting total that is fetched from sql it shows the prevoius month data also with current month also.. not getting any error but it shows the data. my need is need to calculate the month wise worked hours

  • #751486
    Hi selvi for that only i already gave the above link to refer there is direct soltuion for this requirement you needs to mobify bit in that. other than that it works fine. Please refer that.
    Rajesh B
    To Live More,Learn More

  • #751492
    i never ask for one day. i ask for a month

  • #751496
    The code looks fine, what result you got in your query. your query should return you more than one month record.
    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #751523
    s mr.prasad kulkarni. i need it for one month

  • #751579
    Hi Selvi,

    The code is fine, i don't think any error in it.Can you please let me know the data in the table and the result you are getting. Then i will be able to say where is the exact error.

    Thanks,
    Ashutosh Jha
    http://tricksroad.com

  • #751583
    Hello,

    Try the below code :


    DateTime inTime = Convert.ToDateTime(sdr[0].ToString(), new CultureInfo("en-Gb"));
    DateTime outTime = Convert.ToDateTime(sdr[1].ToString(), new CultureInfo("en-Gb"));
    TimeSpan ts = inTime.Subtract(outTime).Duration();


    for more details check : http://dotnetsquare.com/codesnippets/1040-how-to-find-time-difference-between-two-time-using-C-Sharp-net.aspx

    If it helps you then Rate this.

    Best Regards,
    Manoranjan Sahoo
    http://www.dotnetsquare.com
    Blog: http://blog.msahoo.net

  • #751591
    i have no error it takes the whole entry value.but my need is for one month 01/08/2014 to 31/08/2014

    but it takes 01/08/2014 to 17/09/2014

    so please check my code in sql also it shows the wrong result


  • Sign In to post your comments