Click here to Skip to main content
15,920,383 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.

  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')
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.
// 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
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