首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >所选JQuery日期时间选择器日期与今天的日期不匹配

所选JQuery日期时间选择器日期与今天的日期不匹配
EN

Stack Overflow用户
提问于 2016-05-25 03:04:58
回答 1查看 264关注 0票数 0

我正在处理MVC 5中的一个日程安排项目。我想根据当前日期检查所选的日期。如果它们匹配,则仅在视图中显示今天日期的计划约会。目前,当我添加"appointment.AppointmentDate == DateTime.Now“时,约会不会显示。取而代之的是视图重复了“今天没有约会”。

我通过StackOverFlow和其他网站进行了研究,试图找出答案,但运气不佳。其中一次尝试是在Create视图中添加".data(' Date '):“$(‘#Date’),#datetimepicker2').datetimepicker”,将类型设置为Date,但没有成功。我是一个初学者,希望有人能帮助我正确的方向。谢谢。我的代码如下:

型号:

代码语言:javascript
运行
复制
 public enum AppointmentTime
    {
        _1pm_to_2pm,  ApplicationDbContext _dbContext = new ApplicationDbContext();


    public ActionResult Create(int id)
    {


        Property property = _dbContext.Properties.SingleOrDefault(b => b.PropertyID == id);

        ViewBag.propertyName = property.PropertyName;

        Consultation consultation = new Consultation();

        consultation.PropertyID = id;

        return View(consultation);

    }


    [HttpPost]
    public ActionResult Create(Consultation consultation)
    {
        try
        {
            if (ModelState.IsValid)
            {


                    Property property = _dbContext.Properties.SingleOrDefault(b => b.PropertyID == consultation.PropertyID);

                    property.Consultations.Add(consultation);
                    _dbContext.SaveChanges();
                    return RedirectToAction("Details", "Property", new { id = property.PropertyID });

            }
            return View();
        }
        catch
        {
            return View("Error");
        }
    }

        _2pm_to_3pm,
        _3pm_to_4pm,
        _4pm_to_5pm,
        _5pm_to_6pm
    }


    public class Consultation
    {
        [Key]
        public int AppointmentID { get; set; }

        [ForeignKey("Property")]
        public int PropertyID { get; set; }

        [Display(Name = "Enter your name")]
        public string AppointmentName { get; set; }

        [Required]
        [Display(Name = "Email")]
        public string AppointmentEmail { get; set; }


        [Display(Name = "Select Date")]
        [UIHint("AppointmentDate")]
        [Required]
        public DateTime AppointmentDate { get; set; }

        public AppointmentTime AppointmentTime { get; set; }

        public virtual Property Property { get; set; }
}

控制者:

代码语言:javascript
运行
复制
    [Authorize(Roles = "Admin")]
    public ActionResult AdminAppointmentView(Consultation consultaion)
    {
        var appointments = _dbContext.Consultations.ToList();


            return View(appointments);
    }

创建视图

代码语言:javascript
运行
复制
@model OpenProperties.Models.Consultation

@{
    ViewBag.Title = "Create Appointment";
}



<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css"
      rel="stylesheet">
<link rel="stylesheet" type="text/css" media="screen"
      href="http://tarruda.github.com/bootstrap-datetimepicker/assets/css/bootstrap-datetimepicker.min.css">


@using (Html.BeginForm(new { enctype = "multipart/form-data", id = "form1" }))
{
    @Html.ValidationSummary(true)
    <div>
        @Html.HiddenFor(model => model.PropertyID)
        <br />

        <div class="form-group">
            @Html.LabelFor(m => m.AppointmentName)
            @Html.TextBoxFor(m => m.AppointmentName, new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.AppointmentEmail)
            @Html.TextBoxFor(m => m.AppointmentEmail, new { @class = "form-control" })
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.AppointmentTime, new { @class = "control-label col-md-2" })
            @Html.EnumDropDownListFor(model => model.AppointmentTime)
            @Html.ValidationMessageFor(model => model.AppointmentTime)
        </div>


        <div id="datetimepicker1" class="input-append date">
            @Html.TextBoxFor(m => m.AppointmentDate, "{0:dd/MM/yyyy HH}",
      new { placeholder = "App Date", @class = "dtPicket" })
            <span class="add-on">
                <i data-time-icon="icon-time"
                   data-date-icon="icon-calendar"></i>
            </span>
        </div>
        <br />
        <br />
        <input type="submit" value="Submit" />
    </div>



    <script type="text/javascript"
            src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js">
    </script>
    <script type="text/javascript"
            src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js">
    </script>
    <script type="text/javascript"
            src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.min.js">
    </script>
    <script type="text/javascript"
            src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.pt-BR.js">
    </script>
    <script type="text/javascript">
        $('#datetimepicker1, #datetimepicker2').datetimepicker({
            format: 'dd/MM/yyyy'
        });
    </script>
}

AdminAppointmentView视图:

代码语言:javascript
运行
复制
@model IEnumerable<OpenProperties.Models.Consultation>

<h2>Todays Appointments</h2>

@foreach (var appointment in Model)
{
    if (appointment.AppointmentDate == DateTime.Now)
    {
    <li>
        <ul>@appointment.AppointmentID</ul>
        <ul>@appointment.AppointmentDate</ul>
        <ul>@appointment.AppointmentEmail</ul>
        <ul>@appointment.AppointmentName</ul>
        <ul>For Property ID: @appointment.PropertyID</ul>
    </li>
    }
    else
    {
        <ul> No Appointments Today </ul>
    }
}
EN

Stack Overflow用户

发布于 2016-05-25 03:46:25

乍一看。

if (appointment.AppointmentDate == DateTime.Now) (

}

appointment.AppointmentDate和DateTime.Now的日期格式是否相同??

如果不是,只需将Tostring方法添加到这些日期。

例如。

代码语言:javascript
运行
复制
DateTime.Now.ToString("MMMM dd, yyyy") 

2)您可以查看天数、小时数和分钟数//的差异。日期之间也是如此。而不是比较

代码语言:javascript
运行
复制
DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;

// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Days;

3)您可以在datetime Picker中设置日期格式,如下所示。

代码语言:javascript
运行
复制
$('#datepicker').datetimepicker({
    dateFormat: "yy-mm-dd",
    timeFormat:  "hh:mm:ss"
});

4)另外,您可以将脚本和datepicker代码放在html之前吗

代码语言:javascript
运行
复制
<script type="text/javascript"


     src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script type="text/javascript"
        src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/js/bootstrap.min.js">
</script>
<script type="text/javascript"
        src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.min.js">
</script>
<script type="text/javascript"
        src="http://tarruda.github.com/bootstrap-datetimepicker/assets/js/bootstrap-datetimepicker.pt-BR.js">
</script>
<script type="text/javascript">
    $('#datetimepicker1, #datetimepicker2').datetimepicker({
        format: 'dd/MM/yyyy'
    });
</script>
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37421868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档