SlideShare une entreprise Scribd logo
1  sur  8
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.
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) { } };
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.
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
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.
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; }
Editor Templates
/ Views/Shared/EditorTemplates/
Date.cshtml
DateTime.cshtml
DateTimeNoSetTime.cshtml
Date_TopUnit.cshtml
RangeDateTimeNoSetTime.cshtml
RangeDateTimeToDateTime.cshtml
RangeDateTimeToTime.cshtml
RangeDateToDate.cshtml
RangeTimeToTime.cshtml
Time.cshtml
TimeNoSetTime.cshtml
UiDateTimeModel.cshtml
UiDateTimeRangeModel.cshtml
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.

Contenu connexe

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

En vedette

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
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.