Date format issue with DatePicker asp.net mvc - c#

I have a problem with datepicker format, it sets the date to MM-dd-yyyy. First, my model was set to following date format dd-MM-yyyy, I've changed it to MM-dd-yyyy in my model but nothing happen, the jqueryval script throw an error it wants the dd-MM-yyyy format.
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? dateOut_cash
{
get;
set;
}
So how can I set it to have everything working well ?

Related

mvc view date display format

I have my date data annotation as
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime CreatedOn { get; set; }
In my view:
#Html.DisplayFor(item=> item.CreatedOn)
But my date appears as just: 11 12 2017 in my view, insteaed of 11/12/2017. What ate my /'s? Anything I forgot to include?
In the format-string, wrap the / in single quotes, so your model should look something like this:
[DisplayFormat(DataFormatString = "{0:dd'/'MM'/'yyyy}")]
public DateTime CreatedOn { get; set; }
When rendered on the page, it uses the desired format.
The Documentation on DataFormatString has a remark about formatting of dates, but doesn't mention anything about this issue of formatting forward-slashes. Their proposed solution about setting HtmlEncode = true didn't work for me. I found the solution in the alternative suggestion on the answer for this similar question.
It seems everything boils down to Culture info. As it currently stands it doesn't seem like we can specify CultureInfo in DisplayFormat, so i ended up defining a reusable helper method:
public static string FormatDate(this IHtmlHelper helper, DateTime date)
{
var formattedDate = string.Format(CultureInfo.InvariantCulture, "{0:dd/MM/yyyy}", date);
return formattedDateWithTime;
}
and in my view:
#Html.FormatDate(Model.CreatedOn)

format datetime object inside ajax.actionlink

I have a view where there are ajax.actionlinks, some of these action links need to display a date property of the model and I have the date property as follows:
[Display(Name = "Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Date { get; set; }
however, because ajax.actionlink accepts a string for its first argument, I can't use a lambda expression :
m => m.Date
rather I'm using
Model.Date.ToString()
but this isn't showing the formatting I want. I've tried doing
Model.Date.ToString("MM-dd-yyyy");
but I'm getting red underline because its not recognizing the ToString overload with 1 argument... any ideas on how I can get this to work?
Since Model.Date is nullable, you need to access the Value of the DateTime? before using that version of ToString:
Model.Date.HasValue ? Model.Date.Value.ToString("MM-dd-yyyy") : null;

MVC Date Validation Fails on return to page

I have a model property setup as below
[DisplayName("Target Date")]
[DateValidInFormat("dd/MM/yyyy", ErrorMessage = "Invalid Date (dd/mm/yyyy)")]
public string TargetDate { get; set; }
On the UI the date validation works fine when the page is posted but when control returns to the browser the field is marked with the error message. I can delete the value and re-enter it and it works fine. It only fails for dates in the future.

C# DateTime Default Value Issue 01/01/0001 00:00:00

I am having trouble inserting a DateTime into a database with the following error:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
I am aware that the SQL date must be between 1/1/1753 12:00:00 AM and 12/31/9999, however my date seems to remain at the 01/01/0001 00:00:00.
I have the following date defined in a web service method:
[DataMember]
public DateTime RecordTimeStamp { get; set; }
This is used in the following code to add into database
sqlComm.Parameters.Add("#RecordTimeStamp", SqlDbType.DateTime).Value = pCustomer.RecordTimeStamp;
This code takes its value from an aspx page with code
DateTime now = DateTime.Now;
pCustomer.RecordTimeStamp = now;
I am trying to get this to insert the current date into the database, but it doesn't seem to change from the default.
Try to change the property
from:
[DataMember]
public DateTime RecordTimeStamp { get; set; }
to:
[DataMember]
public DateTime? RecordTimeStamp { get; set; }
Hopefully this will fix the issue. What happens is DateTime is NOT NULL data type and it enforces to put a default value there (01/01/0001) to make sure that non-null date will be submitted. I don't know why it does not accept altered value and throws out an "out of range" exception. That is something you might want to investigate further.

Model property formatting

I have a model with a DateTime propery:
[DisplayName("Updated")]
public DateTime lastUpdatedDate { get; set; }
At the moment, I think I am incorrectly handling the formatting of the datetime in the view.
<tr>
<td>#Html.LabelFor(m=>m.lastUpdatedDate)</td>
<td>#Html.Label(Model.lastUpdatedDate.ToLongDateString())</td>
</tr>
I am sure this is wrong. Firstly, should I do the formatting in the model, and return string (In the model used for displaying the date - the Update model needs the DateTime type for the control)? But it gets complicated - timezones. Should I manipulate the value of the date time (based on a timezone selection by the user on registration) in the model on the get; .. thing? (What's the called? The getter?? hehe).
Just trying to make my code friendly to work with, while I learn MVC.
If you want to elegantly deal with timezones, I suggest you read this answer. For simple formatting the DateTime property in your model, decorate it with the [DisplayFormat] attribute:
[DisplayName("Updated")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime lastUpdatedDate { get; set; }
and in your view:
#Html.DisplayFor(x => x.lastUpdatedDate)

Categories