-1

I have a time span string as

1.21:00:00

it means 45 hours and i need it as

45:00:00

and i use following method from Need only HH:MM:SS in time span string

it works perfectly for this above problem but when i change the string as

 1.21:30:00

then mentioned code return me one hour more than actual time. in this case it return me 46:29:00 but actual that i need is 45:29:00.

I am using c#

EDIT

public Tuple<string,string>  Calculate_Hours(DateTime strt, DateTime end)
{
    TimeSpan wrkhrs = new TimeSpan(0, 0, 0);
    TimeSpan exthrs=new TimeSpan(0,0,0);
    cmd = new SqlCommand("select * from vw_Rept_Attend where UserID ='"+Convert.ToInt32 (cmbEmp.SelectedValue)+"' and convert(date,AtnDate) between '"+strt.Date+"' and '"+end.Date+"'",conn);
    conn.Open();
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        if (dr["WorkHrs"].ToString().Length>0)
           wrkhrs=wrkhrs.Add(Convert.ToDateTime(dr["WorkHrs"].ToString()).TimeOfDay);
       // exthrs = exthrs.Add(Convert.ToDateTime(dr["ExtraHrs"].ToString()).TimeOfDay);

        if (!dr["ExtraHrs"].ToString().Contains("-") && dr["ExtraHrs"].ToString().Length > 0)
        {
            exthrs = exthrs.Add(Convert.ToDateTime(dr["ExtraHrs"].ToString()).TimeOfDay);
        }
        else if (dr["ExtraHrs"].ToString().Contains("-") && dr["ExtraHrs"].ToString().Length > 0)
        {
            //int index = dr["ExtraHrs"].ToString().LastIndexOf('-');
            //string rhs = dr["ExtraHrs"].ToString().Substring(index + 1);
            string ext = dr["ExtraHrs"].ToString().Substring(dr["ExtraHrs"].ToString().LastIndexOf("-") +1);
            //TimeSpan test = Convert.ToDateTime(dr["ExtraHrs"].ToString()).TimeOfDay.Negate();
            exthrs = exthrs.Subtract(Convert.ToDateTime(ext).TimeOfDay);
        }
    }
    conn.Close();
    dr.Close();
   // string tst = exthrs.ToString().Substring(exthrs.ToString().LastIndexOf("-") + 1);
   // wrkhrs.ToString();
   // exthrs.ToString();
    var val1 = TimeSpan.Parse(wrkhrs.ToString());
   string val3= string.Format("{0}:{1:mm}:{1:ss}", Math.Floor(TimeSpan.Parse(wrkhrs.ToString()).TotalHours), TimeSpan.Parse(wrkhrs.ToString()));
    var val2=TimeSpan.Parse(exthrs.ToString());
    string val4=string.Format("{0}:{1:mm}:{1:ss}", Math.Floor(TimeSpan.Parse(exthrs.ToString()).TotalHours), TimeSpan.Parse(exthrs.ToString()));
    return new Tuple<string, string>(string.Format("{0}:{1:mm}:{1:ss}", Math.Floor(TimeSpan.Parse(wrkhrs.ToString()).TotalHours), TimeSpan.Parse(wrkhrs.ToString())), string.Format("{0}:{1:mm}:{1:ss}", Math.Floor(TimeSpan.Parse(exthrs.ToString()).TotalHours), TimeSpan.Parse(exthrs.ToString())));
   // MessageBox.Show(wrkhrs.ToString()+exthrs.ToString());
}

this is what i did yet.

Community
  • 1
  • 1
Waqas
  • 847
  • 4
  • 14
  • 36

1 Answers1

0

I'm not sure if i got it since your expected result is strange. You add 30 minutes but expect 29 minutes in the result string? If i ignore that this should work:

var ts = TimeSpan.Parse("1.21:30:00");
string result = string.Format("{0:00}:{1:00}:{2:00}", (int)ts.TotalHours, ts.Minutes, ts.Seconds);

Returns: 45:30:00

You can use Math.Abs if you want to handle negative timespans:

String.Format("{0:00}:{1:00}:{2:00}", (int)ts.TotalHours, Math.Abs(ts.Minutes), Math.Abs(ts.Seconds));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • and what about if input time span is in negative form? i mean if time will be "-1.21:30:00" – Waqas Jul 09 '15 at 10:34
  • @Waqas: what is your expected result then? I have edited my answer, you can use `Math.Abs` to remove the minus sign on the minutes or seconds. – Tim Schmelter Jul 09 '15 at 10:38
  • i checked it with negative sign it returns "-45:-29:00" i need only 1 negative sign at the beginning of string i-e "-45:29:00". can you please do it for me? – Waqas Jul 09 '15 at 10:38
  • @Waqas: as mentioned, that is already handled by my second approach. – Tim Schmelter Jul 09 '15 at 10:39
  • thanks, i add Math.abs to that. Now its fine, Thanks for you time. – Waqas Jul 09 '15 at 10:40