5

How do I covert JavaScript string "5:00 PM" to DateTime or TimeSpan when it get send to the MVC controller. I am using

bootstrap-timepicker

 // usage      
 <script type="text/javascript">
        $('#timepicker1').timepicker();
 </script>

Javascript payload

{
  Skip: 0
  Status: []
  Take: 15
  DueTime: "1:00 PM" // keep in mind that this is a string
}

Server Object would be something like

class TimeSheet
{
   public TimeSpan DueTime;
}
1
  • A TimeSpan is representative of a span between two times -- a delta or difference -- it is not meant to actually store point-in-time information. Your DueTime property should, IMO, be a DateTime struct. You could pass the string to the server and parse it on the server side using the method shown in this question. Commented Jan 20, 2014 at 21:16

2 Answers 2

2

Use DateTime.Parse. Convert on server(on controller) when your string would transmit with your time. http://msdn.microsoft.com/ru-ru/library/system.datetime.parse(v=vs.110).aspx

Sign up to request clarification or add additional context in comments.

1 Comment

there should be a SO language filter that converts MSDN links to the locale of the logged in user :)
0

Okay so I read everyhting wrong hence the deleted answer.. !

But I'm not giving up ;)

Your bootstrap-timepicker, will give you a time as this "1:00 PM".

But before that we are going to look on the serverside to see what we can parse into a datetime object.

This is C# for parsing datetime.

string dateString, format;  
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;

dateString = "15/08/2000 16:58"
format = "dd/MM/yyyy HH:mm"
result = DateTime.ParseExact(dateString, format, provider);

Now as you se your string wont look like that I'm going to assume that you want todays date!

This is function I tend to use most of the times when converting, to 24H clock.

function ConvertTimeformat(str) {
    var time = str;
    var hours = Number(time.match(/^(\d+)/)[1]);
    var minutes = Number(time.match(/:(\d+)/)[1]);
    var AMPM = time.match(/\s(.*)$/)[1];
    if (AMPM == "PM" && hours < 12) hours = hours + 12;
    if (AMPM == "AM" && hours == 12) hours = hours - 12;
    var sHours = hours.toString();
    var sMinutes = minutes.toString();
    if (hours < 10) sHours = "0" + sHours;
    if (minutes < 10) sMinutes = "0" + sMinutes;

    //Creating the todays date in the right format
     var today = new Date();
     var dd = today.getDate();
     var mm = today.getMonth()+1;//January is 0!`
     var yyyy = today.getFullYear();
     if(dd<10){dd='0'+dd}
     if(mm<10){mm='0'+mm}
     var todaysdate = dd+'/'+mm+'/'+yyyy +" " ; //<--I added an extra space!
     var hoursNminutes = sHours + ":" + sMinutes
     //CREATE THE RIGHT FORMAT FOR DATE.PARSEXACT "dd/MM/yyyy HH:mm"
      var dateToParse = todaysdate + hoursNminutes 
    return dateToParse;
}

To Use the function do like this!

//Call it and provide your bootstrap time. And make it return to a variable
var dateToBeSentToServer = ConvertTimeformat("1:00 PM");
//OR With the bootstraptime as a variable 
var dateToBeSentToServer = ConvertTimeformat(timevariable);

Now you can send dateToBeSentToServer to your serverside for parsing!

3 Comments

Could you explain how you're going to get from "1:00 PM" to epoch time?
I am using bootstrap-timepicker, would would I do it inside bootstrap function?
I don't want to send it to the server side as a string that just seems like a dirty work around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.