Use of the bootstrap datetimepicker control in the asp.net mvc4 project, mvc4datetimepicker
Some time ago I wrote an article about calling Alibaba's SMS interface to develop the SMS Group Sending notification function. In this article, the datetimepicker control of bootstrap is used. Currently, bootstrap is widely used. In addition, the project uses the bootstrap framework to quickly build the interface, in this way, selecting this date control also saves resources, and here it is presented:
Dependencies and bootstrap-datetimepicker.js, but before introducing resources, you need BundleConfig in asp.net mvc. cs slightly changed. It should be noted that this date control depends on jquery. js and bootstrap. js. These resource references sometimes have to be prioritized. jquery should be referenced first. js and then bootstrap. js is bootstrap-datetimepicker.js. If the control does not respond when you click it, you can use Firefox to view the page errors on the console.
After the resource is referenced, the html structure is not mentioned here. You need to know that there is an example html in the downloaded control zip package, which facilitates operations. The easy-to-input requirement is now solved, and the formatting is another option. Fortunately, the date and time obtained by the date control is normalized, but there is a problem that the selected time is in English, whether or not localization is required is also a consideration.
The time required for the project is in the format of October 04. In this case, the Chinese character is not considered. The string is processed in the background code to obtain the expected time and date. Here you can use ajax to submit the selected date. Perform the following operations in the action:
Public ActionResult SmsSendForMeeting (FormCollection Form) {string Operator = Form ["Operator"]; // sender string Department = Form ["Department"]; // Department name string Time = Form ["Time"]. toString (); // set the Time // process the date // Date Format: "18 September 2016-pm" Time = sp. dateParse (Time); // call the date processing function // omit other irrelevant code return View ();}
The DateParse () method is used here, which is the method for converting the date format. Here there are two ideas: one is to remove the year and space, and the other is to convert the English month to a digital month, that is, the string operation.
Public string DateParse (string Time) {// string Time = "19 January 2016-pm"; Time = Time. replace ("", ""); // remove the space string day = Time. substring (0, 2) + "day"; // retrieve "day" int c = Time. lastIndexOf (":")-2; string DetailTime = Time. substring (c); // the hour-minute int B = Time. lastIndexOf ("201"); // This Is Not 2016 Time = Time. remove (B); string month = Time. remove (0, 2); // month switch (month) {case "January": month = "January"; break; case "February": month = "February "; break; case "March": month = "March"; break; case "Limit l": month = "April"; break; case "May": month = "May "; break; case "June": month = "June"; break; case "July": month = "July"; break; case "August": month = "August "; break; case "September": month = "September"; break; case "October": month = "October"; break; case "November": month = "November "; break; case "December": month = "December"; break;} string DateTime = month + day + DetailTime; // get-> September 18 return DateTime ;}
Here are the switch () Functions and string operations used, such as substring, replace, indexof, and remove. There are many ways to process strings from left to right. The last returned result of the DateParse () function is July 22, October 4, which satisfies the requirements for "formatting" and "Easy-to-Enter" regular message dates. If my ideas and methods are incorrect, please correct me and ask for advice with an empty mind.