Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI All,
I have a model like this:

C#
public class Employees{

      public int Id {get;set;}
      public string Name {get;set;}
      public DateTime StartTime {get;set;}
      public DateTime FinishTime {get;set;}

}


Then I have a webapi controller returning the details against a searched employee like this:

C#
public class TimeController: ApiController
{
      [Route("api/getuserhours")]
        [HttpGet]
        public IEnumerable<Employees> GetDefaultTime()
        {
        
           var getEmp = context.Employees.Where(x => x.Name ==             currentUser).ToList();

         // now this is where i'm stuck .. i just want my model StartTime and //FinishTime  in particular to only return the time only not date. so how would i //access the getEmp here and change the datetime to return the time only .
           return getEmp;

   }


} 



So before the results are sent to the view , how would i go about in changing the datetime to only return time?

Thank you .

What I have tried:

I was hopping to access the getEmp like this:
getEmp.StartTime.TooString("set time here") but nope this throws errors
Posted
Updated 12-May-16 4:01am
Comments
Sinan Ergin 12-May-16 9:24am    
Hmm. Interesting.

// var getEmp = context.Employees.Where(x => x.Name == currentUser).ToList();
// return getEmp; This type Employee! Cuz you dont select specify type.

I'll try change. Cuz I'm not quit what you want.

var getEmp = context.Employees.Where(x=>x.Name == currentUser).Select(x=> new { Start = x.StartTime,Finish = x.FinishTime }).AsEnumerable();

IEnumerable<*TimeStamp*> result = (TimeStamp)(from emp in getEmp select emp);

return result;

Thanks in advance.

DateTime can't hold just a time, the clue is in the class name :)

Just return the DateTime as it is and your view can convert the value to a string when it shows it, just ignoring the date part and showing only the time.
 
Share this answer
 
var getEmp = context.Employees.Where(x=>x.Name == currentUser).Select(x=> new { Start = x.StartTime.ToString("HH:mm"),Finish = x.FinishTime.ToString("HH:mm") }).AsEnumerable();

You have other options also for this:

DateTime dt = DateTime.Parse("6/22/2009 07:00:00 AM");

dt.ToString("HH:mm"); // 07:00 // 24 hour clock // hour is always 2 digits
dt.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // hour is always 2 digits
dt.ToString("H:mm"); // 7:00 // 24 hour clock
dt.ToString("h:mm tt"); // 7:00 AM // 12 hour clock
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900