c# - Convert Javascript time string to DateTime or TimeSpan in MVC Controller - Stack Overflow

How do I covert JavaScript string "5:00 PM" to DateTime or TimeSpan when it get send to the M

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;
}

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;
}
Share Improve this question edited Jan 23, 2014 at 5:06 BPX 8861 gold badge8 silver badges20 bronze badges asked Jan 20, 2014 at 21:11 eugenekgneugenekgn 1,7222 gold badges19 silver badges40 bronze badges 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. – Cᴏʀʏ Commented Jan 20, 2014 at 21:16
Add a ment  | 

2 Answers 2

Reset to default 2

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

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744896831a4599745.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信