日期时间分析问题
本文关键字:问题 时间 日期 | 更新日期: 2023-09-27 18:30:44
我在验证日期时间文本框时遇到问题,
日期时间是按以下方式显示日期:
public class Article : TblAbstract<int> {
private DateTime displayDate;
[DisplayName("وقت الظهور")] // Display date in arabic
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd hh:mm:ss tt}")]
[DataType(DataType.DateTime)]
[Editable(true, AllowInitialValue = true)]
// the error message means: display date is not in a correct format and it must be 2016-09-30 06:26:35 am
[DateTimeFormatValidation(ErrorMessage = "وقت الظهور ليس بالشكل الصحيح، عليك إعتماد هذا الشكل 2016-09-30 06:26:35 am")]
public DateTime DisplayDate {
get { return displayDate; }
set { displayDate = value; }
}
}
public class AddArticleViewsModel : AdminGViewsModel {
private List<ArticleItem> articleItemList;
private Article newArticle;
public AddArticleViewsModel() : base(typeof(AddArticleViewsModel)) {
newArticle = new Article();
newArticle.DisplayDate = DateTime.Now;
articleItemList = new List<ArticleItem>();
}
public List<ArticleItem> ArticleItemList {
get { return articleItemList; }
set { articleItemList = value; }
}
public Article NewArticle {
get { return newArticle; }
set { newArticle = value; }
}
}
我正在对文本框使用此 jquery 控件:
jquery-ui-timepicker-addon-1.6.1.js 来自网站
http://trentrichardson.com/examples/timepicker
使用的 JavaScript:
<script>
$("#txtDisplayDate").datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm:ss tt'
});
</script>
文本框的 HTML 和 Razor:
<div class="lblMsg">
// arabic means: warning you to fix the following issues
@Html.ValidationSummary(false, "تنبيه، عليك إصلاح الأخطاء التالية:")
</div>
<p>
@Html.LabelFor(model => model.NewArticle.DisplayDate, new { id = "lblDisplayDate", name = "lblDisplayDate", Class = "lblAddArticle", For = "txtDisplayDate" })
@Html.TextBoxFor(model => model.NewArticle.DisplayDate, "{0:yyyy-MM-dd hh:mm:ss tt}", new { id = "txtDisplayDate", name = "txtDisplayDate", Class = "inputAddArticle" })
</p>
这适用于所有浏览器,除了火狐(火狐43.04 64位版)
请参阅这些图像:
存在问题的已设计页面的图片:i.stack.imgur.com/EMLa4.png
显示火狐中发生的问题的图像:i.stack.imgur.com/UGKRc.png
该问题的解决方案是使用:剃刀:
@Html.TextBoxFor(model => model.NewArticle.DisplayDate, "{0:yyyy-MM-dd hh:mm:ss tt}", new { id = "txtDisplayDate", name = "txtDisplayDate", Class = "inputAddArticle", @Type="datetime", @Data_Val_Date = "وقت الظهور ليس بالنسق المطلوب، عليك إعتماد هذا الشكل 2016-01-30 11:30:20 am" })
JavaScript:
$(function () {
jQuery.validator.methods.date = function (value, element) {
if (value) {
try {
var dateTest = /^[1-2][0-9]{3}-[0-1][1-9]-[0-3][0-9] [0-1][1-9]:[0-5][0-9]:[0-5][0-9] [APap][mM]$/;
return this.optional(element) || dateTest.test(value);
} catch (ex) {
alert(ex);
return false;
}
}
return true;
};
}