With an enterprise level application that has both users and organizations spread across multiple timezones, the handling of date and time conversions seems to be a universal challenge. While this problem is not unique to C# it just happened that the project I am currently working on is a C# Web Application (ASP.NET, MVC 3).
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
C# ASP.Net MVC DateTime Conversions
1. DateTime Conversions
in
C# .NET MVC
This short presentation will cover the basics
of using this project for DateTime
conversions in your own solution.
This is not in any way meant to be an
exhaustive coverage of the extensibility
points of C# .NET MVC that are leveraged
to make this possible.
2. UiDateTimeModel
The UiDateTimeModel is at the core of everything DateTime. There are a set of properties
specifically for use in the Views and a single property that is primarily used in the Server Side code.
Available Properties for use in the Views:
Views/Forms
DateTimeLocalValue - DateTime?
LocalDate -String
LocalTime -String
TimeZoneName -String: Required Property in ALL forms posting a UiDateTimeModel
NoSetTime -Bool
View Only Properties (String Properties)
LocalDateTime
LocalDateTimeAbreviatedMonthName
LocalDateTimeDayOfMonth
LocalDateTimeDayWithFullDate
Flags:
ImplicitlySet -Bool: used to indicate that either the Date or Time of the DateTimeLocalValue was set via the
model binding and NOT via a form post or other end user activities.
Please note that the UiDateTimeModel requires a TimeZone at the time of instantiation:
var model = new UiDateTimeTestModel { UiDateTime = new UiDateTimeModel(timeZone) { } };
3. UiDateTimeRangeModel
Closely related to the UiDateTimeModel and potentially used as extensively throughout many
applications is the UiDateTimeRangeModel. The two most important (and only) properties of
this model are the StartDateTime and EndDateTime which are both of type
UiDateTimeModel.
Additionally you will want to note that there are three different constructors which improve the
usability of the UiDateTimeRangeModel especially when this model must be instantiated for
mapping purposes.
Constructors:
UiDateTimeRangeModel(string timeZoneName)
UiDateTimeRangeModel(string timeZoneName, DateTime? startDateUtc, DateTime? endDateUtc)
UiDateTimeRangeModel(string timeZoneName, DateTime? startDateUtc, DateTime? endDateUtc, bool
isAllDay = false)
Please note that there is NO default constructor allowing instantiation of this model without a
TimeZone.
4. ValidationAttributes
Below you will find the ValidationAttributes which will setup both the Clientside
Unobtrusive validation and the Server side validation. They all have very
descriptive names.
UiDateTimeFormatDateValidation
UiDateTimeFormatTimeValidation
UiDateTimeGreaterThanDateAttributeOrNullValidation
UiDateTimeGreaterThanDateAttributeValidation
UiDateTimeGreaterThanTimeAttributeValidation
UiDateTimeMaxLimitDateValidation
UiDateTimeNotInFutureValidation
UiDateTimeNotInPastValidation
UiDateTimeRequiredIfAttributeValidation
UiDateTimeRequiredIfAttributeValueEqualsValidation
UiDateTimeRequiredIfAttributeValueNotEqualsValidation
UiDateTimeRequiredIfNotAttributeValidation
UiDateTimeRequiredValidation
5. ValidationAttributes
Implementation
UiDateTimeModel
[UiDateTimeFormatDateValidation("LocalDate", ErrorMessageResourceName =
"DateFormatValid", ErrorMessageResourceType = typeof(Resources.Validation))]
[UiDateTimeFormatTimeValidation("LocalTime", ErrorMessageResourceName =
"TimeFormatValid", ErrorMessageResourceType = typeof(Resources.Validation))]
[UiDateTimeMaxLimitDateValidation("LocalDate", 50, ErrorMessageResourceName =
"DateMustBeLessThanYearsInFuture", ErrorMessageResourceType = typeof(Resources.Validation))]
public class UiDateTimeModel : IComparable
CampaignModel.cs
[UiDateTimeDisplayAttribute("StartDateTime.LocalDate", "StartDate", typeof(Resources.UI) )]
[UiDateTimeDisplayAttribute("EndDateTime.LocalDate", "EndDate", typeof(Resources.UI))]
[UiDateTimeNotInPastValidation("StartDateTime.LocalDate", ErrorMessageResourceName =
"DateNotInPastValid", ErrorMessageResourceType = typeof(Resources.Validation))]
[UiDateTimeGreaterThanDateAttributeOrNullValidation("EndDateTime.LocalDate", "StartDateTime.LocalDate", f
alse, ErrorMessageResourceName = "DateMustBeAfterDateValid", ErrorMessageResourceType =
typeof(Resources.Validation))]
public UiDateTimeRangeModel CampaignDateRange { get; set; }
Note: This ValidationAttributes work the same as any other validation attributes that we use on model
properties with one important difference. That being the extra parameter that defines the property of the
model where a given attribute will be applied.
6. ValidationAttributes
Implementation continued
By far the most commonly used attribute that you will implement is the
UiDateTimeDisplayAttribute which is used when creating a model property
of type UiDateTimeModel along with the set the Display Name for a given
UiDateTimeModel property.
As seen below we are setting the display name of
UiDateTimeModel.LocalDate to be the Resource property
“CancellationEffective”.
[UiDateTimeDisplayAttribute("LocalDate", "CancellationEffective", type
of(Resources.Admin))]
public UiDateTimeModel CancellationEffective { get; set; }
8. Editor Template
Implementation
ViewsSomeFile.cshtml
<div class="formrow">
@Html.EditorFor(m => m.ActionDate, "DateTimeNoSetTime", new
{
LocalDate = new { @class = "w75" },
LocalTime = new { @class =
"w65", disabled="disabeld" }
})
</div>
ViewsActionYetAnotherFile.cshtml
@Html.EditorFor(m => m.ScheduledDateTime, "RangeDateTimeNoSetTime", new
{
StartDateTime_LocalDate = new { @class = "w75", id ="ScheduledAction_Date"
},
StartDateTime_LocalTime = new { @class = "w65" }
})
Note: In both implementations of the Editor Templates above the third property of the
template defines and HTML object that will be used to add additional attributes to the
model property defined.