Swedish SSN Regular expression reject users under a specific age - c#

I Have a problem with my regular expression. I have made it possible to valdiate correct swedish social security number to match these criteria.
YYMMDDNNNN
YYMMDD-NNNN
YYYYMMDDNNNN
YYYYMMDD-NNNN
But i would also like to reject a user to register if the user is under 18 years old. My reqular expression is looking like this at the moment: Do anyone encountered the same problem with the age range Swedish SSN?
private const string RegExForValidation =
#"^(\d{6}|\d{8})[-|(\s)]{0,1}\d{4}$";
UPDATE
private const string RegExForValidation = #"^(?<date>\d{6}|\d{8})[-\s]?\d{4}$";
string date = Regex.Match("19970918-1234", RegExForValidation).Groups["date"].Value;
DateTime dt;
[Required(ErrorMessage = "Du måste ange personnummer")]
[RegularExpression(RegExForValidation, ErrorMessage = "Personnummer anges med 10 siffror (yymmddnnnn)")]
public string PersonalIdentityNumber { get; set; }
Second Update
public class ValidateOfAge : ValidationAttribute
{
public bool IsOfAge(DateTime birthdate)
{
DateTime today = DateTime.Today;
int age = today.Year - 18;
if (birthdate.AddYears(birthdate.Year) < today.AddYears(-age))
return false;
else
return true;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string RegExForValidation = #"^(?<date>\d{6}|\d{8})[-\s]?\d{4}$";
string date = Regex.Match((string)value, RegExForValidation).Groups["date"].Value;
DateTime dt;
if (DateTime.TryParseExact(date, new[] { "yyMMdd", "yyyyMMdd" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
if (IsOfAge(dt))
return ValidationResult.Success;
return new ValidationResult("Personnummer anges med 10 siffror (yymmddnnnn)");
}
}

This is a case where I wouldn't use regular expressions, but instead rely on the base class library's built-in DateTime parsing functionality:
public DateTime GetBirthDate(string ssn)
{
var strippedOfSerial =
ssn.Substring(0, ssn.Length - 4).TrimEnd('-');
return DateTime.ParseExact(
strippedOfSerial,
new[] { "yyMMdd", "yyyyMMdd" },
new CultureInfo("se-SE"),
DateTimeStyles.None);
}
Now you can look at the returned DateTime value and compare it to DateTime.Now in order to figure out if you want to reject it or not.
In my opinion, this is much more readable than relying on regular expressions, and may be safer and more flexible as well. As you can tell, you can e.g. use other cultures etc. to tweak the parsing strategy.

You need to get the birthdate from the SSN, parse to DateTime, and then compare with today's date.
Here is the method checking if a person is of age:
public bool IsOfAge(DateTime birthdate)
{
DateTime today = DateTime.Today; // Calculating age...
int age = today.Year - birthdate.Year;
if (birthdate > today.AddYears(-age))
age--;
return age < 18 ? false : true; // If the age is 18+ > true, else false.
}
And here is how you can use this:
string RegExForValidation = #"^(?<date>\d{6}|\d{8})[-\s]?\d{4}$";
string date = Regex.Match("19970918-1234", RegExForValidation).Groups["date"].Value;
DateTime dt;
if (DateTime.TryParseExact(date, new[] { "yyMMdd", "yyyyMMdd" }, new CultureInfo("sv-SE"), DateTimeStyles.None, out dt))
Console.WriteLine(IsOfAge(dt));
Note that [-|(\s)] matches a -, |, (, whitespace or ). I am sure you only want to match a hyphen or whitespace.
I added a named capture to the regex and removed unnecessary symbols from the character class. Also, note that {0,1} is the same as ?.
UPDATE
To make it work in an MVC app, you need to implement a custom validator:
[Required(ErrorMessage = "Du måste ange personnummer")]
[ValidateOfAge] // <---------------------------- HERE
public string PersonalIdentityNumber { get; set; }
And implement this as follows:
public class ValidateOfAge: ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string RegExForValidation = #"^(?<date>\d{6}|\d{8})[-\s]?\d{4}$";
string date = Regex.Match((string)value, RegExForValidation).Groups["date"].Value;
DateTime dt;
if (DateTime.TryParseExact(date, new[] { "yyMMdd", "yyyyMMdd" }, new CultureInfo("sv-SE"), DateTimeStyles.None, out dt))
if (IsOfAge(dt))
return ValidationResult.Success;
return new ValidationResult("Personnummer anges med 10 siffror (yymmddnnnn)");
}
}

Instead of doing custom RegEx, I'd suggest looking at a NuGet package available that is able to parse a valid Swedish Personal Identity Number and then extract information such as DateOfBirth, Age, Gender etc that could be used as input to your validation. The method .GetAge() would be the most relevant in your case. Disclaimer: I'm one of the co-authors of that package.
An implementation of ValidateOfAge using SwedishPersonalIdentityNumber could look like:
public class ValidateOfAge : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (SwedishPersonalIdentityNumber.TryParse((string)value, out var personalIdentityNumber))
{
if(personalIdentityNumber.GetAge() >= 18) {
return ValidationResult.Success;
} else {
return new ValidationResult("Du måste vara minst 18 år gammal.");
}
}
return new ValidationResult("Ogiltigt personnummer.");
}
}
SwedishPersonalIdentityNumber supports the patterns you describe, including a few more that is valid (did you know that a PIN can contain a + for example?).
You can find the source code here:
https://github.com/ActiveLogin/ActiveLogin.Identity
And the packages on NuGet here:
https://www.nuget.org/packages/ActiveLogin.Identity.Swedish/
https://www.nuget.org/packages/ActiveLogin.Identity.Swedish.AspNetCore/
The second one contains a validation attribute for validating a PIN in your model.
If the use case of validating age is common enough, we could implement it into that validation attribute. I created an issue to find out about interest and the design.
The API design proposed at the moment is this (for your scenario):
[SwedishPersonalIdentityNumber(MinimumAge = 18)]
public string PersonalIdentityNumber { get; set; }
Please share your thoughts in the issue if this is something you'd like to see implemented.

Related

mvc-model date range annotation dynamic dates

with reference to this thread in stackoverflow
[Range(typeof(DateTime), "1/2/2004", "3/4/2004",
ErrorMessage = "Value for {0} must be between {1} and {2}")]
public DateTime EventOccurDate{get;set;}
I tried to add some dynamic dates into my model's date range validator as:
private string currdate=DateTime.Now.ToString();
private string futuredate=DateTime.Now.AddMonths(6).ToString();
[Range(typeof(DateTime),currdate,futuredate,
ErrorMessage = "Value for {0} must be between {1} and {2}")]
public DateTime EventOccurDate{get;set;}
But Error Occurs.Is there no way to set dynamic date range validation in MVC?
You cannot use dynamic values in attributes because they are metadata that is generated at compile-time. One possibility to achieve this is to write a custom validation attribute or use Fluent Validation which allows for expressing more complex validation scenarios using a fluent expressions.
Here's an example of how such custom validation attribute might look like:
public class MyValidationAttribute: ValidationAttribute
{
public MyValidationAttribute(int monthsSpan)
{
this.MonthsSpan = monthsSpan;
}
public int MonthsSpan { get; private set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
var date = (DateTime)value;
var now = DateTime.Now;
var futureDate = now.AddMonths(this.MonthsSpan);
if (now <= date && date < futureDate)
{
return null;
}
}
return new ValidationResult(this.FormatErrorMessage(this.ErrorMessage));
}
}
and then decorate your model with it:
[MyValidation(6)]
public DateTime EventOccurDate { get; set; }

Send variables to other classes

Can I have a function that checks if true or false and send my verbal to other classes?
I tried:
public class Func
{
public static bool CheckDate(string number)
{
string new_number = number.ToString();
if (new_number.Length==8)
{
string yyyy = new_number.Substring(0, 4);
string mm = new_number.Substring(4, 2);
string dd = new_number.Substring(6, 2);
return true;
}
else
{
return false;
}
}
}
I want to send the verbal yyyy, mm, dd to my Program.cs class.
What should I do?
Don't reinvent wheels, use the DateTime.TryParseExact method which is built specifically for this purpose. Forget about regexes and substrings when you are dealing with dates in the .NET framework:
public static bool CheckDate(string number, out DateTime date)
{
return DateTime.TryParseExact(number, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
}
And now as you can see defining the CheckDate becomes kinda meaningless because it already exists in the BCL. You would simply use it like this:
string number = "that's your number coming from somewhere which should be a date";
DateTime date;
if (DateTime.TryParseExact(
number,
"dd/MM/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out date
))
{
// the number was in the correct format
// => you could use the days, months, from the date variable which is now a DateTime
string dd = date.Day.ToString();
string mm = date.Month.ToString();
string yyyy = date.Year.ToString();
// do whatever you intended to do with those 3 variables before
}
else
{
// tell the user to enter a correct date in the format dd/MM/yyyy
}
UPDATE:
Since I got a remark in the comments section that I am not actually answering the question, you could use a similar approach to the one I recommend. But please, promise me you will never write a code like this, it's just for illustration of the TryXXX pattern.
define a model:
public class Patterns
{
public string DD { get; set; }
public string MM { get; set; }
public string YYYY { get; set; }
}
and then modify your CheckDate method so that it sends an out parameter:
public static bool CheckDate(string number, out Patterns patterns)
{
patterns = null;
string new_number = number.ToString();
if (new_number.Length == 8)
{
Patterns = new Patterns
{
YYYY = new_number.Substring(0, 4),
MM = new_number.Substring(4, 2),
DD = new_number.Substring(6, 2)
}
return true;
}
else
{
return false;
}
}
which you could use like this:
string number = "that's your number coming from somewhere which should be a date";
Patterns patterns;
if (CheckDate(numbers, out patterns)
{
string dd = patterns.DD;
string mm = patterns.MM;
string yyyy = patterns.YYYY;
// do whatever you intended to do with those 3 variables before
}
else
{
// tell the user to enter a correct date in the format dd/MM/yyyy
}
The CheckDate function’s aim is to check if a date is valid. Don’t introduce crappy side effects: write another function that actually sends your stuff to the object you want.
If you want to check if a string is a date, do it in CheckDate.
When you know a string is a date, extract the date elements you want from it through such a ExtractDateElem function, but please, no side-effects.
you have to declare your variables as below...
public static string yyyy;
public static string mm ;
public static string dd ;
Or
protected static string yyyy;
protected static string mm ;
protected static string dd ;
as per your need and depends where your program.cs file is...

Regular Expression stop crashing

Consider this code:
[Required]
[RegularExpression(#"\d{2,2}/\d{2,2}/\d{4,4} \d{2,2}:\d{2,2}:\d{2,2}",
ErrorMessage = "Wrong Syntax Entered, Needed:day/Month/Year Hour:Minutes:Seconds")]
public DateTime Posted { get; set; }
When I enter this value, my application crashes: 00/00/0000 00:00:00
Is there a way to stop this and make it more realistic? I'm wanting to allow the date so it only allows max 31 days or less up 1 and allows max months up 12 and min 1?
A regex is the wrong way to validate dates and times. Use DateTime.TryParse instead.
EDIT:
Here's an example:
using System;
using System.Globalization;
...
bool valid;
DateTime dt;
valid = DateTime.TryParseExact(inputString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
I agree with #MRAB that TryParse is probably easier to manage and maintain.
This SO question also tries to do what you're doing and they created a custom attribute (that derives from RegularExpressionAttribute) that seems to have solved his problem. Maybe it will help you.
Hope this helps.
Yet another, change FormatException message in ModelBinder(but going to far...).
DefaultModelBinder.GetValueInvalidResource is static method.I can not override this method. Because I create CustomModelBinder class and override SetProperty method.
[Required]
[AdditionalMetadata(
"PropertyValueInvalid",
"Wrong Syntax Entered, Needed:day/Month/Year Hour:Minutes:Seconds")]
public DateTime? Posted { get; set; }
And create custom ModelBinder
public class CustomModelBinder : DefaultModelBinder
{
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
{
base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
var propertyMetadata = bindingContext.PropertyMetadata[propertyDescriptor.Name];
var invalidMessage = propertyMetadata.AdditionalValues.ContainsKey("PropertyValueInvalid")
? (string)propertyMetadata.AdditionalValues["PropertyValueInvalid"]
: string.Empty;
if (string.IsNullOrEmpty(invalidMessage))
{
return;
}
// code from DefaultModelBinder
string fullPropertyKey = CreateSubPropertyName(bindingContext.ModelName, propertyDescriptor.Name);
if (!bindingContext.ValueProvider.ContainsPrefix(fullPropertyKey))
{
return;
}
ModelState modelState = bindingContext.ModelState[fullPropertyKey];
foreach (ModelError error in modelState.Errors.Where(err => String.IsNullOrEmpty(err.ErrorMessage) && err.Exception != null).ToList())
{
for (Exception exception = error.Exception; exception != null; exception = exception.InnerException)
{
if (exception is FormatException)
{
string displayName = propertyMetadata.GetDisplayName();
string errorMessageTemplate = invalidMessage;
string errorMessage = String.Format(CultureInfo.CurrentCulture, errorMessageTemplate,
modelState.Value.AttemptedValue, displayName);
modelState.Errors.Remove(error);
modelState.Errors.Add(errorMessage);
break;
}
}
}
}
}
How about this?
Validating datatime with Regex will result in complex regex like
^([0][1-9]||[1-2][0-9]||[3][0-1])/([0][1-9]||[1][1-2])/([1][0-9]{3}) ([0][1-9]||[1][0-2]):([0][1-9]||[1-5][0-9]):([0][1-9]||[1-5][0-9])$
But Still i doubt it may miss some edge cases.
If you are using MVC3, then the best way to Use Self validating model like follows,
public class TestModel:IValidatableObject
{
string MyDateTime{get;set;}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
List<ValidationResult> v = new List<ValidationResult>();
DateTime dt = default(DateTime);
DateTime.TryParseExact(MyDateTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture,DateTimeStyles.None,out dt);
if (dt.Equals(default(DateTime)))
v.Add(new ValidationResult("Invalid Date time"));
return v;
}
}

How to remove time portion of date in C# in DateTime object only?

I need to remove time portion of date time or probably have the date in following format in object form not in the form of string.
06/26/2009 00:00:00:000
I can not use any string conversion methods as I need the date in object form.
I tried first converting the DateTime to a string, remove the time specific date from it, but it adds 12:00:00 AM as soon as I convert it back to DateTime object back again.
Use the Date property:
var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;
The date variable will contain the date, the time part will be 00:00:00.
You can use format strings to give the output string the format you like.
DateTime dateAndTime = DateTime.Now;
Console.WriteLine(dateAndTime.ToString("dd/MM/yyyy")); // Will give you smth like 25/05/2011
Read more about Custom date and time format strings.
Use the method ToShortDateString. See the documentation http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx
var dateTimeNow = DateTime.Now; // Return 00/00/0000 00:00:00
var dateOnlyString = dateTimeNow.ToShortDateString(); //Return 00/00/0000
Have a look at the DateTime.Date property.
Gets the date component of this instance.
The Date property will return the date at midnight.
One option could be to get the individual values (day/month/year) separately and store it in the type you want.
var dateAndTime = DateTime.Now;
int year = dateAndTime.Year;
int month = dateAndTime.Month;
int day = dateAndTime.Day;
string.Format("{0}/{1}/{2}", month, day, year);
None of the above answers solved my problem on winforms.
the easiest way to reach ONLY date is the simple function in Datetime:
DateTime dt = DateTime.now;
String BirthDate = dt.ToShortDateString();
You will only have date in Birthday string .
Try to make your own Structure for that. DateTime object will have date and time both
You can't. A DateTime in .NET always have a time, defaulting to 00:00:00:000. The Date property of a DateTime is also a DateTime (!), thus having a time defaulting to 00:00:00:000 as well.
This is a shortage in the .NET Framework, and it could be argued that DateTime in .NET violates the Single Responsibility Principle.
The easiest way is something like this and it will return only the date:
var date = DateTime.Now.ToShortDateString();
Here is another method using String.Format
DateTime todaysDate = DateTime.UtcNow;
string dateString = String.Format("{0:dd/MM/yyyy}", todaysDate);
Console.WriteLine("Date with Time: "+ todaysDate.ToString());
Console.WriteLine("Date Only : " + dateString);
Output:
Date with Time: 9/4/2016 11:42:16 AM
Date Only : 04/09/2016
This also works if the Date Time is stored in database.
For More Date and Time formatting check these links:
Reference 1
Reference 2
Hope helps.
DateTime.Date
var newDate = DateTime.Now; //newDate.Date property is date portion of DateTime
This way of get only date without time
DateTime date = DateTime.Now;
string Strdateonly = date.ToString("d");
Output = 5/16/2015
Use date.ToShortDateString() to get the date without the time component
var date = DateTime.Now
var shortDate = date.ToShortDateString() //will give you 16/01/2019
use date.ToString() to customize the format of the date
var date = DateTime.Now
var shortDate = date.ToString('dd-MMM-yyyy') //will give you 16-Jan-2019
Since .NET 6 / C# 10 you can do this:
var dateOnly = DateOnly.FromDateTime(dateTime);
I wrote a DateOnly structure. This uses a DateTime under the skin but no time parts are exposed publically:
using System;
public struct DateOnly : IComparable, IFormattable, IComparable<DateOnly>, IEquatable<DateOnly>
{
private DateTime _dateValue;
public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}
DateOnly otherDateOnly = (DateOnly)obj;
if (otherDateOnly != null)
{
return ToDateTime().CompareTo(otherDateOnly.ToDateTime());
}
else
{
throw new ArgumentException("Object is not a DateOnly");
}
}
int IComparable<DateOnly>.CompareTo(DateOnly other)
{
return this.CompareToOfT(other);
}
public int CompareToOfT(DateOnly other)
{
// If other is not a valid object reference, this instance is greater.
if (other == new DateOnly())
{
return 1;
}
return this.ToDateTime().CompareTo(other.ToDateTime());
}
bool IEquatable<DateOnly>.Equals(DateOnly other)
{
return this.EqualsOfT(other);
}
public bool EqualsOfT(DateOnly other)
{
if (other == new DateOnly())
{
return false;
}
if (this.Year == other.Year && this.Month == other.Month && this.Day == other.Day)
{
return true;
}
else
{
return false;
}
}
public static DateOnly Now()
{
return new DateOnly(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
}
public static bool TryParse(string s, ref DateOnly result)
{
DateTime dateValue = default(DateTime);
if (DateTime.TryParse(s, out dateValue))
{
result = new DateOnly(dateValue.Year, dateValue.Month, dateValue.Day);
return true;
}
else
{
return false;
}
}
public static DateOnly Parse(string s)
{
DateTime dateValue = default(DateTime);
dateValue = DateTime.Parse(s);
return new DateOnly(dateValue.Year, dateValue.Month, dateValue.Day);
}
public static DateOnly ParseExact(string s, string format)
{
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dateValue = default(DateTime);
dateValue = DateTime.ParseExact(s, format, provider);
return new DateOnly(dateValue.Year, dateValue.Month, dateValue.Day);
}
public DateOnly(int yearValue, int monthValue, int dayValue) : this()
{
Year = yearValue;
Month = monthValue;
Day = dayValue;
}
public DateOnly AddDays(double value)
{
DateTime d = new DateTime(this.Year, this.Month, this.Day);
d = d.AddDays(value);
return new DateOnly(d.Year, d.Month, d.Day);
}
public DateOnly AddMonths(int months)
{
DateTime d = new DateTime(this.Year, this.Month, this.Day);
d = d.AddMonths(months);
return new DateOnly(d.Year, d.Month, d.Day);
}
public DateOnly AddYears(int years)
{
DateTime d = new DateTime(this.Year, this.Month, this.Day);
d = d.AddYears(years);
return new DateOnly(d.Year, d.Month, d.Day);
}
public DayOfWeek DayOfWeek
{
get
{
return _dateValue.DayOfWeek;
}
}
public DateTime ToDateTime()
{
return _dateValue;
}
public int Year
{
get
{
return _dateValue.Year;
}
set
{
_dateValue = new DateTime(value, Month, Day);
}
}
public int Month
{
get
{
return _dateValue.Month;
}
set
{
_dateValue = new DateTime(Year, value, Day);
}
}
public int Day
{
get
{
return _dateValue.Day;
}
set
{
_dateValue = new DateTime(Year, Month, value);
}
}
public static bool operator == (DateOnly aDateOnly1, DateOnly aDateOnly2)
{
return (aDateOnly1.ToDateTime() == aDateOnly2.ToDateTime());
}
public static bool operator != (DateOnly aDateOnly1, DateOnly aDateOnly2)
{
return (aDateOnly1.ToDateTime() != aDateOnly2.ToDateTime());
}
public static bool operator > (DateOnly aDateOnly1, DateOnly aDateOnly2)
{
return (aDateOnly1.ToDateTime() > aDateOnly2.ToDateTime());
}
public static bool operator < (DateOnly aDateOnly1, DateOnly aDateOnly2)
{
return (aDateOnly1.ToDateTime() < aDateOnly2.ToDateTime());
}
public static bool operator >= (DateOnly aDateOnly1, DateOnly aDateOnly2)
{
return (aDateOnly1.ToDateTime() >= aDateOnly2.ToDateTime());
}
public static bool operator <= (DateOnly aDateOnly1, DateOnly aDateOnly2)
{
return (aDateOnly1.ToDateTime() <= aDateOnly2.ToDateTime());
}
public static TimeSpan operator - (DateOnly aDateOnly1, DateOnly aDateOnly2)
{
return (aDateOnly1.ToDateTime() - aDateOnly2.ToDateTime());
}
public override string ToString()
{
return _dateValue.ToShortDateString();
}
public string ToString(string format)
{
return _dateValue.ToString(format);
}
public string ToString(string fmt, IFormatProvider provider)
{
return string.Format("{0:" + fmt + "}", _dateValue);
}
public string ToShortDateString()
{
return _dateValue.ToShortDateString();
}
public string ToDbFormat()
{
return string.Format("{0:yyyy-MM-dd}", _dateValue);
}
}
This is converted from VB.NET, so apologies if some conversions are not 100%
I'm surprised no one has mentioned DateTime.Today
var date = DateTime.Today;
// {7/1/2014 12:00:00 AM}
See MSDN
If you are converting it to string, you can easily do it like this.
I'm taking date as your DateTime object.
date.ToString("d");
This will give you only the date.
You Can Try This for the Only Date From the Datetime
String.Format("{0:d/M/YYYY}",dt);
Where dt is the DateTime
Came across this post when trying to solve the original Q.
I am using Asp.Net and after some research I have found when you are binding to the value of the date in code behind, you can drop the time so it will not display on screen.
C#:
DateTime Today = DateTime.Now;
aspx:
<%: this.Today.ToShortDateString() %>
use
DateTime.Now.ToString("dd-MM-yyyy");
I know this is an old post with many answers, but I haven't seen this way of removing the time portion. Suppose you have a DateTime variable called myDate, with the date with time part. You can create a new DateTime object from it, without the time part, using this constructor:
public DateTime(int year, int month, int day);
Like this:
myDate = new DateTime(myDate.Year, myDate.Month, myDate.Day);
This way you create a new DateTime object based on the old one, with 00:00:00 as time part.
You can use this simple code below.
Code: DateTime.Now.ToShortDateString();
Ex.
Console.WriteLine(DateTime.Now.ToShortDateString());
This is for C#10 and above where now a DateOnly and TimeOnly format is available. Using below format, you can extract DateOnly from a DateTime format.
DateOnly myDateNoTime = DateOnly.FromDateTime(DateTime.Now);
string dt = myCalender.SelectedDate.ToString();
string date = dt.Remove(10);
displayDate.Content = date;
If you take date from calender, with this we also get time. Which is not required all time. Using this we can remove time from date.
in my experience none of the said solutions worked, maybe because I wanted to remove the time from extracted date from database, but the code below worked fine:
var date = target_date.Value.ToString("dd/MM/yyyy");
Declare the variable as a string.
example :
public string dateOfBirth ;
then assign a value like :
dateOfBirth = ((DateTime)(datetimevaluefromDB)).ToShortDateString();
This could be simply done this way:
var dateOnly = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day)
Create a struct that holds only the properties you want. Then an extension method to easily get that struct from an instance of DateTime.
public struct DateOnly
{
public int Day { get; set; }
public int Month { get; set; }
public int Year { get; set; }
}
public static class DateOnlyExtensions
{
public static DateOnly GetDateOnly(this DateTime dt)
{
return new DateOnly
{
Day = dt.Day,
Month = dt.Month,
Year = dt.Year
};
}
}
Usage
DateTime dt = DateTime.Now;
DateOnly result = dt.GetDateOnly();
To get only the date portion use the ToString() method,
example:
DateTime.Now.Date.ToString("dd/MM/yyyy")
Note:
The mm in the dd/MM/yyyy format must be capitalized
Add Date property to the DateTime variable
var dateTime = DateTime.Now
var onlyDate = dateTime.Date
Or You can use DataType annotation as well.
[DataType(DataType.Date)]
public DateTime dateTime {get; set;}
The DataType annotation is inside the System.ComponentModel.DataAnnotations namespace.

Data Annotation Ranges of Dates

Is it possible to use [Range] annotation for dates?
something like
[Range(typeof(DateTime), DateTime.MinValue.ToString(), DateTime.Today.ToString())]
I did this to fix your problem
public class DateAttribute : RangeAttribute
{
public DateAttribute()
: base(typeof(DateTime), DateTime.Now.AddYears(-20).ToShortDateString(), DateTime.Now.AddYears(2).ToShortDateString()) { }
}
Docs on MSDN says you can use the RangeAttribute
[Range(typeof(DateTime), "1/2/2004", "3/4/2004",
ErrorMessage = "Value for {0} must be between {1} and {2}")]
public datetime Something { get; set;}
jQuery validation does not work with [Range(typeof(DateTime),"date1","date2"] --
My MSDN doc is incorrect
Here is another solution.
[Required(ErrorMessage = "Date Of Birth is Required")]
[DataType(DataType.Date, ErrorMessage ="Invalid Date Format")]
[Remote("IsValidDateOfBirth", "Validation", HttpMethod = "POST", ErrorMessage = "Please provide a valid date of birth.")]
[Display(Name ="Date of Birth")]
public DateTime DOB{ get; set; }
The simply create a new MVC controller called ValidationController and past this code in there. The nice thing about the "Remote" approach is you can leverage this framework to handle any kind of validations based on your custom logic.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
namespace YOURNAMESPACEHERE
{
public class ValidationController : Controller
{
[HttpPost]
public JsonResult IsValidDateOfBirth(string dob)
{
var min = DateTime.Now.AddYears(-21);
var max = DateTime.Now.AddYears(-110);
var msg = string.Format("Please enter a value between {0:MM/dd/yyyy} and {1:MM/dd/yyyy}", max,min );
try
{
var date = DateTime.Parse(dob);
if(date > min || date < max)
return Json(msg);
else
return Json(true);
}
catch (Exception)
{
return Json(msg);
}
}
}
}
For those rare occurrences when you are forced to write a date as a string (when using attributes), I highly recommend using the ISO-8601 notation.
That eliminates any confusion as to whether 01/02/2004 is january 2nd or february 1st.
[Range(typeof(DateTime), "2004-12-01", "2004-12-31",
ErrorMessage = "Value for {0} must be between {1} and {2}")]
public datetime Something { get; set;}
I use this approach:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
internal sealed class DateRangeAttribute : ValidationAttribute
{
public DateTime Minimum { get; }
public DateTime Maximum { get; }
public DateRangeAttribute(string minimum = null, string maximum = null, string format = null)
{
format = format ?? #"yyyy-MM-dd'T'HH:mm:ss.FFFK"; //iso8601
Minimum = minimum == null ? DateTime.MinValue : DateTime.ParseExact(minimum, new[] { format }, CultureInfo.InvariantCulture, DateTimeStyles.None); //0 invariantculture
Maximum = maximum == null ? DateTime.MaxValue : DateTime.ParseExact(maximum, new[] { format }, CultureInfo.InvariantCulture, DateTimeStyles.None); //0 invariantculture
if (Minimum > Maximum)
throw new InvalidOperationException($"Specified max-date '{maximum}' is less than the specified min-date '{minimum}'");
}
//0 the sole reason for employing this custom validator instead of the mere rangevalidator is that we wanted to apply invariantculture to the parsing instead of
// using currentculture like the range attribute does this is immensely important in order for us to be able to dodge nasty hiccups in production environments
public override bool IsValid(object value)
{
if (value == null) //0 null
return true;
var s = value as string;
if (s != null && string.IsNullOrEmpty(s)) //0 null
return true;
var min = (IComparable)Minimum;
var max = (IComparable)Maximum;
return min.CompareTo(value) <= 0 && max.CompareTo(value) >= 0;
}
//0 null values should be handled with the required attribute
public override string FormatErrorMessage(string name) => string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, Minimum, Maximum);
}
And use it like so:
[DateRange("2004-12-01", "2004-12-2", "yyyy-M-d")]
ErrorMessage = "Value for {0} must be between {1} and {2}")]
I found issues with the [Range(typeof(DateTime)] annotation and would describe it as "clunky at best" it leaves too much to chance IF it works.
Remote validation seems to be a good way of: avoiding javascript in views and maintaining server side code integrity, personally never like sending code to a client to execute if I can avoid it.
Using #StackThis answer as a base and reference to an article on remote validation in MVC3
Model
public class SomeDateModel
{
public int MinYears = 18;
public int MaxYears = 110;
[Display(Name = "Date of birth", Prompt = "e.g. 01/01/1900")]
[Remote(action: "ValidateDateBetweenYearsFromNow", controller: "Validation", areaReference: AreaReference.UseRoot, AdditionalFields = "MinYears,MaxYears", HttpMethod = "GET" ,ErrorMessage = "Subject must be over 18")]
public DateTime? DOB { get; set; }
}
Controller - Deployed at the root directory
namespace Controllers
{
public class ValidationController : Controller
{
[HttpGet]
[ActionName("ValidateDateBetweenYearsFromNow")]
public JsonResult ValidateDateBetweenYearsFromNow_Get()
{
//This method expects 3 parameters, they're anonymously declared through the Request Querystring,
//Ensure the order of params is:
//[0] DateTime
//[1] Int Minmum Years Ago e.g. for 18 years from today this would be 18
//[2] int Maximum Years Ago e.g. for 100 years from today this would be 100
var msg = string.Format("An error occured checking the Date field validity");
try
{
int MinYears = int.Parse(Request.QueryString[1]);
int MaxYears = int.Parse(Request.QueryString[2]);
//Use (0 - x) to invert the positive int to a negative.
var min = DateTime.Now.AddYears((0-MinYears));
var max = DateTime.Now.AddYears((0-MaxYears));
//reset the response error msg now all parsing and assignmenst succeeded.
msg = string.Format("Please enter a value between {0:dd/MM/yyyy} and {1:dd/MM/yyyy}", max, min);
var date = DateTime.Parse(Request.QueryString[0]);
if (date > min || date < max)
//switch the return value here from "msg" to "false" as a bool to use the MODEL error message
return Json(msg, JsonRequestBehavior.AllowGet);
else
return Json(true, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
return Json(msg, JsonRequestBehavior.AllowGet);
}
}
}
}
The msg variable is displayed as part of the Html helper ValidationSummary or the Html helper ValidationFor(x=>x.DATETIME)
View
It's important to note that the fields passed as parameter 2 and 3 must exist in the view in order for the remote validation to pass the values to the controller:
#Html.EditorFor(m => m.DOB)
#Html.HiddenFor(m => m.MinYears)
#Html.HiddenFor(m => m.MaxYears)
#Html.ValidationSummary()
The model and Html helpers will do all the jquery work for you.

Categories

Resources