Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my ASP.Net MVC project I am using the TimePicker plugin to render time.


$.ajax({
  url: "/payroll/PostPayrollEmpSheet",
  type: "POST",
  data: {
    'ProjectId': $('.counterPayroll').data('projectid'),
    'PayrollEmployeeType': 1,
    'DayNumber': $('.counterPayroll').data('daynumber'),
    'EmployeeName': empName,
    'PreBreakClockIn': preBreakTimeClockIn,
    'PreBreakClockOut': preBreakTimeClockOut,
    'PostBreakClockIn': postBreakTimeClockIn,
    'PostBreakClockOut': postBreakTimeClockOut
  }
})


In my Action method in backend expects DateTime datatype. How do I parse hh:mm time to DateTime and send via AJAX?


What I have tried:

I tried to do something like this also:

'PreBreakClockIn': new Date('1970-01-01T' + preBreakTimeClockIn + 'Z'),
'PreBreakClockOut': new Date('1970-01-01T' + preBreakTimeClockOut + 'Z')
Posted
Updated 24-Feb-20 21:20pm

1 solution

You could use one of DateTime structure's constructors:
DateTime Constructor (System) | Microsoft Docs[^]

Or, since the datetime picker most probably returns you a TimeSpan, you could just add it to specified date at 00:00:00. You can add a TimeSpan value to a DateTime value to get a new DateTime.
C#
// Assuming preBreakTimeClockIn, preBreakTimeClockOut, postBreakTimeClockIn and preBreakTimeClockOut are TimeSpan values
DateTime baseDate = DateTime.Today; // or any other relevant value
DateTime preIn = baseDate + preBreakTimeClockIn;
DateTime preOut = baseDate + preBreakTimeClockOut;
DateTime postIn = baseDate + postBreakTimeClockIn;
DateTime postOut = baseDate + postBreakTimeClockOut;
 
Share this answer
 
Comments
istudent 25-Feb-20 8:45am    
When I passed time span value I could not format them to display as e.g. 7:00 am. That's why I had to go with date time data type.
phil.o 25-Feb-20 10:23am    
What do you mean, you could not format? What have you tried, and how was it inacceptable?

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