how i can get current date without time on MVC view - model-view-controller

Curently i am using this code then i submit my data to controller and through controller i add to my database
#Html.HiddenFor(model => model.date, new { #Value=DateTime.Now.ToShortDateString() })

If you do not want to show your time value, then you could do that by specifying the string format you want your DateTime to show in your ToString():
#Html.HiddenFor(model => model.date, new { #Value=DateTime.Now.ToString("dd-MM-yyyy") })
dd-MM-yyyy format will only show day-Month-year elements of the DateTime. To show hour-minutes-seconds, use HH-mm-ss or hh-mm-ss. For more info, check MSDN article on standard and custom date time format.

Related

Filter Kendo Grid column with Date picker

I have a Kendo MVC Grid that has a column with a DateTime column. Everything looks good and formats correctly. When i filter, it gives me a date picker and a time picker. When i remove the DateTimeFilter Template below and use template contains, it will give me a Date picker (which i want), but still wants to filter by the date and time.. Is there a way i can have the date and time all as the same field, but only filter with a Date picker.? Example: i use the Date picker to pick 07/24/2017 and it filter everything on that date regardless of time.. Or do they need to be completely different fields, or even concatenated fields in the same column.?
the Column data looks as such: 07/24/2017 18:12:00
columns.Bound(c => c.CreatedDate).Title("Submitted On")
.ClientTemplate("#= kendo.toString(kendo.parseDate(CreatedDate), 'MM/dd/yyyy HH:mm:ss') #")
.Filterable(ftb => ftb.Cell(cell => cell.Template("DateTimeFilter")));
If you're using MVC, in your model, you add DataType.Date above your DatePicker property as below:
[DataType(DataType.Date)]
public DateTime SubmittedOn{ get; set; }
Note: add reference to System.ComponentModel.DataAnnotations if its not included in your header.

DatePicker format issue

I'm trying to use Kendo UI DatePicker
#(Html.Kendo().DatePickerFor(model => model.CalledInAt).Format("dd/MM/yyyy HH:mm tt").Value(DateTime.Now).HtmlAttributes(new { #style = "width:205px" }))
I'm using ASP.NET MVC 4 with entity framework.
when i try to submit a form it gives me the error. "The value '19/09/2013 04:08 AM' is not valid for CalledInAt"
If i change the format to dd/MM/yyyy then it works. something like below works.
#(Html.Kendo().DatePickerFor(model => model.CalledInAt).Format("MM/dd/yyyy HH:mm tt").Value(DateTime.Now).HtmlAttributes(new { #style = "width:205px" }))
what would be the reason?
Try to set the culture in web config that match with date format.
<globalization uiCulture="en" culture="en-AU" />
See this link ASP.NET Date time support for different cultures

How to filter a Telerik Mvc Grid bound to a formatted datetime property

New to Telerik Mvc, so I'm hoping I'm missing the obvious here. I am using version 2011.3.1115.340 of Telerik Mvc. The grid is being databound via ajax calls to controller actions and then the columns are hooked in to public properties. About as boilerplate as you can get.
Html.Telerik().Grid<ProductResult>()
.Name("Grid")
.DataBinding(databinding => databinding.Ajax()
.Select("GetProductInfo", "Product"))
.DataKeys(keys => keys.Add(a => a.ProductId))
.Columns(columns => {
columns.Bound(a => a.CreateDate).Width(30);
columns.Bound(a => a.Sales).Width(30);
columns.Bound(a => a.Service).Width(30);
columns.Bound(a => a.Training).Width(30);
columns.Bound(a => a.ModifiedDate).Width(30);
})
.Pageable(p => p.PageSize(30))
.Sortable()
.Filterable()
.Groupable()
All filtering, aside from date values, works fine. The bound class is ultimately delivered from a wcf service where the datetime values are formatted in the data contract
[DataMember]
[DisplayName("Last Modified Date")]
[DisplayFormat(DataFormatString = "{0:d}")]
public virtual DateTime ModifiedDate { get; set; }
When I use the built-in grid filter (dropdown) and enter a date value that I know exists in the datasource and can see in the grid, I get no results - an empty grid. I've read posts that seem to indicate that this should work like a charm, but obviously not in my case. Any idea what I'm doing wrong here?
columns.Bound(a => a.ModifiedDate).Format("{0:d}").Width(30);
I'm guessing your Date field "Modified Date" is a DateTime field. This is caused because the datepicker filter in KendoUI can't match up the time portion of your DateTime values. Since nothing exactly matches the values in your database, it returns nothing.
I recently battled this as well and my best solution was to cast the DateTime fields in SQL to a Date when I was selecting the fields with my SPROC. Yours would look something like this, assuming you are using SQL.
SELECT CAST(ModifiedDate AS date) AS ModifiedDate FROM YourTable
That fixed my problem of the filter as it was now returning the rows I wanted.
This exposed another problem for me as now my date values were all getting offset by minus six hours because we were hosting our site on Azure. To remedy that problem I followed exactly this from the Kendo team:
Kendo - Using UTC time on both client and server sides

MVC 3 Razor and the display of null dates in forms

I have an MVC 3 website where I display dates in forms using:
<div class="editor-label control-label">
#Html.LabelFor(m => m.rrsfWoman.DateOfBirth)
</div>
<div class="editor-field controls">
#Html.EditorFor(m => m.rrsfWoman.DateOfBirth)
#Html.ValidationMessageFor(m => m.rrsfWoman.DateOfBirth)
</div>
The date of birth is defined in the rrsfWoman class as
public DateTime WomansDateOfBirth { get; set; }
My problem is that as the date of birth field has by default a value of MinDate. Is there a way I can supress the display of the date as 1/01/0001 without making the field nullable.
Thanks
Is there a way I can supress the display of the date as 1/01/0001
without making the field nullable.
The correct way to achieve that is to make the date nullable in your view model. If you don't do that later you will struggle with model binding as well because a non-nullable DateTime field cannot be bound to an empty string and you will have to write custom model binders and stuff to make it work. You will make your life miserable if you don't use view models.
This being said, if you want to go against good practices, you could define a custom editor template for the DateTime type that will perform the check and use an empty value but honestly I don't recommend you doing that:
~/Views/Shared/EditorTemplates/DateTime.cshtml:
#model DateTime
#if (Model == default(DateTime))
{
#Html.TextBox("", "")
}
else
{
#Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue)
}
You could set the default value of WomansDateOfBirth explicitly like so:
private DateTime _womansDateOfBirth = DateTime.Now;
public DateTime WomansDateOfBirth
{
get { return _womansDateOfBirth; }
set { _womansDateOfBirth = value; }
}
Where DateTime.Now is your MinDate.
If the date field does not allow nulls and you want to force the user to enter a date.
IMO, the best way is to simply clear the input field after the form is built.
So add this little snip-it at the end of your view.
<script type='text/javascript'>
document.getElementById("DateOfBirth").value = "";
</script>

Issue To fetch date format At Edit() In ASP.NET MVC 3

When I edit my one of the form sin ASP.NET MVC 3 in this when I edit a user registration form then I got date in startdate is 21-Mar-12 12:00:00 AM in text box but I need 21-Mar-12.
So how can I format textbox date like that?
You could decorate the DateTime property on your view model with the [DisplayFormat] attribute which allows you to specify a given format:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yy}")]
public DateTime SomeDate { get; set; }
and in your strongly typed view use the EditorFor helper to render the corresponding input field:
#Html.EditorFor(x => x.SomeDate)
As far as binding the value back to a DateTime field when posting back is concerned you could write a custom model binder that will use this format. I have shown an example here.

Resources