Timepicker save 12h to 24 hours Format

I'm having problem on saving the value of the Time Picker of Kendo to 24 Hours Format , Timepicker shows "HH:mm tt" format but i want to convert it to "HH:mm:ss" , I use Time Span for my drowdown list

Sample Codes

String clientShiftId = formCollection["clientShiftId"];
            String clientId = formCollection["clientId"];
            String dateShift = formCollection["dllShiftDay"];
            String startTime = formCollection["txtStartTime"];
            String endTime = formCollection["txtEndTime"];
            var stayHere = formCollection["stayHere"];

            Client_Customer_Position_Shift clientCusPosShift = new Client_Customer_Position_Shift();
            try
            {

                if (String.IsNullOrWhiteSpace(clientShiftId) || clientShiftId == "0")
                {
                    client.Client_Customer_PositionID = Convert.ToInt32(clientId);
                    clientCusPosShift.Day_LookID = Convert.ToInt32(dateShift);
                    DateTime parsed = DateTime.ParseExact(endTime.Trim(), "hh:mm tt",CultureInfo.InvariantCulture);
                    client.EndTime = parsed.ToString("HH:mm:ss", CultureInfo.InvariantCulture);  <------- Line of Error 
DateTime parse = DateTime.ParseExact(startTime.Trim(), "hh:mm tt",CultureInfo.InvariantCulture);
                    client.StartTime = parse.ToString("HH:mm:ss", CultureInfo.InvariantCulture);  <------- Line of Error 

Answers


You can't parse it as a TimeSpan if it has an AM/PM designator. You could use:

DateTime parsed = DateTime.ParseExact(endTime.Trim(), "hh:mm tt",
                                      CultureInfo.InvariantCulture);

// If you need a string
client.EndTime = parsed.ToString("HH:mm:ss", CultureInfo.InvariantCulture);

// If you just need a TimeSpan
client.EndTime = parsed.TimeOfDay;

I'm assuming that the value you get will always be in the invariant culture? You should also consider using DateTime.TryParseExact instead of ParseExact, in order to detect invalid input more cleanly.

Note the "hh" instead of "HH" when parsing, by the way - you'll receive 11pm as "11:00 pm" rather than "23:00 pm". Also note how I'm using a local variable for the intermediate value - I would recommend that rather than repeatedly setting the same property (client.EndTime) which could lead to confusion when debugging.

(As an aside, you could also use my Noda Time library for this, which has a separate LocalTime type, which is more appropriate here as you don't have a date. I wouldn't suggest it for just this case, but if you're doing other date/time work in the app you may find it useful.)


Need Your Help

Render a partial everytime on ajax call

ruby-on-rails ruby ajax

I want to run these three lines of code everytime when an ajax calls is made to an action in ruby on rails via ajax..

Simplify Select Query by Multiple Where Condition

sql sql-server select where

I have select query on SQL Server 2012, with multiple where condition, here's my code :