-1

enter image description here

suppose A is my Table and inside this table i have one column like times inside column name times i have n number of times Example :-1st:-02:30 2nd:-03:25 3rd:-00:45 i want output like TotalTime=06:40

i got out put using jquery but i want how to do inside a controller using foreach loop please help me

my controller code:-

[HttpPost]
    public ActionResult getTimeSheetByBasit(DateTime? CurrentDate, string ActivityTime)
    //obj.UserDocumentList = ThidDb.UserDocument.Where(x => x.CreatedBy == UserId).ToList();
    {
        VM_TimeSheet ObjVM_TimeSheet = new VM_TimeSheet();
        int LoggedUser = User.KEY();
        string LoggedUserName = User.UserName();
        string UserEmail = User.EmailID();
        DateTime TimeIn, TimeOut;
        string TimeInn, TimeOuut, TotalTime;

        //code add here fot adding text box time with total houres enter by user select time Sheet
        using (SecurenetDB SecurenetDB = new SecurenetDB())
        {
            ObjVM_TimeSheet.TimesheetList = SecurenetDB.AD_TimeSheet.Where(x => DbFunctions.TruncateTime(x.ActivityDate.Value) == DbFunctions.TruncateTime(CurrentDate) && x.UserKEY == LoggedUser).ToList();
            TimeIn = SecurenetDB.AD_CardPunching.Where(x => DbFunctions.TruncateTime(x.EventDate) == DbFunctions.TruncateTime(CurrentDate) && x.UserName == LoggedUserName).Select(x => x.Time_In).FirstOrDefault();
            TimeOut = SecurenetDB.AD_CardPunching.Where(x => DbFunctions.TruncateTime(x.EventDate) == DbFunctions.TruncateTime(CurrentDate) && x.UserName == LoggedUserName).Select(x => x.Time_Out).FirstOrDefault();
            TimeInn = TimeIn.ToString("hh:mm tt");
            TimeOuut = TimeOut.ToString("hh:mm tt");
            TotalTime = SecurenetDB.AD_CardPunching.Where(x => DbFunctions.TruncateTime(x.EventDate) == DbFunctions.TruncateTime(CurrentDate) && x.UserName == LoggedUserName).Select(x => x.TotalHours).FirstOrDefault();
            // ObjVM_TimeSheet.TimesheetList=SecurenetDB.AD_TimeSheet.Where(x=>x.Hours== TextTime && x.UserKEY == LoggedUser).ToList();
            var sum = "00:00";
            foreach(var iteam in ActivityTime)
            {
                sum = sum + iteam;
               
            }

         
        }
        return Json(new
        {
            TimeSheetData = this.RenderPartialViewToString("TimeSheetData", ObjVM_TimeSheet.TimesheetList),
            TimeIn = TimeInn,
            TimeOut = TimeOuut,
            TotalTime = TotalTime
        }, JsonRequestBehavior.AllowGet);
    }

enter image description here

Ali
  • 1
  • 1

1 Answers1

0

Use TimeSpan

    string[] times = new string[] {"02:30", "03:25", "00:45"};

    TimeSpan totalTime = new TimeSpan(0);
    
    foreach (string time in times)
    {
        TimeSpan ts = TimeSpan.Parse(time);
        totalTime += ts;
    }
    
    Console.WriteLine(totalTime.ToString(@"hh\:mm"));

Output

06:40
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
  • show my image which i uploded thent u get to know..... – Ali Sep 12 '22 at 07:43
  • I did it and all i can get is that you want to sum several time spans. Am I missing something here? – ɐsɹǝʌ ǝɔıʌ Sep 12 '22 at 07:50
  • i want sum of times in a column using foreach loop see my controller – Ali Sep 12 '22 at 08:42
  • How are you receiving your time spans in your controller. Are they contained into `ActivityTime`? Once you have them separately (e.g: into an array) it's easy to sum them by using a `foreach` loop. See my updated answer. – ɐsɹǝʌ ǝɔıʌ Sep 12 '22 at 11:47