[RESOLVED]How to get Date Only without Time in my textbox from Controller ?


i try here to get values from my Controller i got every thing Ok But i have date filed don’t get it as i need – i set format to be "2014-11-28" but i got it as "00:00:00 11/28/2014"

i don’t know where is Err ?

Model Code:

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
        [Required(ErrorMessage = "You Must Enter Date")]
        public DateTime Visit_Date { get; set; }

Controller Code:

        public ActionResult IsOut(int Doctor_ID, DateTime Visit_Date)
            var message = string.Empty;
            var result = new IdentityResult();

            var IsOut = db.Doctor_Out.Where(d => Visit_Date >= d.Frm_Date && Visit_Date <= d.To_Date);

            if (IsOut.Count() != 0)
                message = "Doctor Is Not Here";
                result = new IdentityResult(message);

                return View("_Err", IsOut);
                VisitDatas VS = new VisitDatas();
                VS.Doctor_ID = Doctor_ID;
                VS.Visit_Date = Visit_Date; // Here is Date value i need it as "2014-11-28"
                ViewBag.Clinic_Code_ID = new SelectList(db.ClinicCodes, "Clinic_Code_ID", "Clinic_Code_Name");
                ViewBag.Doctor_ID = new SelectList(db.Doctors, "Doctor_ID", "Doctor_Name");
                ViewBag.Kashf_ID = new SelectList(db.Kashfs.OrderBy(m => m.Kashf_Name), "Kashf_ID", "Kashf_Name");
                return RedirectToAction("Create", VS);

View Code:

                <td style="width: 120px; text-align: left; font-size: medium; font-weight: bold; vertical-align:middle">Date:</td>
                    @Html.TextBoxFor(model => model.Visit_Date, "{0:yyyy-MM-dd}", new { @class = "form-control", style = "width: 150px; font-size: medium; font-weight: bold; text-align: center" })
                    @Html.ValidationMessageFor(model => model.Visit_Date, "", new { @class = "text-danger" })

@section scripts{
    <script src="~/Scripts/jquery-2.1.1.min.js"></script>
    <script src="~/Scripts/jquery-ui.min-1.11.1.js"></script>
    <link href="~/Content/themes/base/all.css" rel="stylesheet" />
        $(function () {
            $("#Visit_Date").datepicker({ dateFormat: "yy-mm-dd" });

So how can i get date in my view as "2014-11-28" without Time ?

// change
VS.Visit_Date = Visit_Date; // Here is Date value i need it as "2014-11-28"
// to
VS.Visit_Date = Visit_Date.ToString("yyyy-MM-dd");

This assumes VS.Visit_Date is a string.  If Visit_Date is also DateTime then your existing code should be used and you should convert to the right format using ToString when the date is shown.

DateTime objects can be easily formatted using either the String.Format() or DateTime.ToString() method by passing in a
formatting string like the ones defined here. If your Visit_Date object is a DateTime object and you want to format it using the "{year}-{month}-{date}" format, you would just use
the appropriate "yyyy-MM-dd" string as seen below :

// This assumes that Visit_Date is a DateTime object and that VS.Visit_Date is a string
VS.Visit_Date = Visit_Date.ToString("yyyy-MM-dd");

If VS.Visit_Date is in fact already a DateTime object, then your current code should be fine. You would just need to format it prior to it being output such as within your View :

<!-- Example -->

Leave a Reply