datetime

Topics: Developer Forum
Sep 26, 2007 at 8:46 PM
Hello,

I am having problems with datetime format validation.
The default date format is mm/dd/yyyy. How do I reset default format to dd/mm/yyyy.


Thanks for your help.

tumay
Coordinator
Sep 26, 2007 at 11:37 PM
Tumay

What method are you using to add rules?

All the attributes (CompareDateTimeRuleAttribute, RangeDateTimeRuleAttribute and RequiredDateTimeRuleAttribute) use the DateTimeConverter. Datetime converter tries the following conversion formats in order “dd MMM yyyy HH:mm:ss.ff", "yyyy-MM-ddTHH:mm:ss", "dd MMM yyyy hh:mm tt", "dd MMM yyyy hh:mm:ss tt", "dd MMM yyyy HH:mm:ss", "dd MMM yyyy HH:mm" and "dd MMM yyyy".


If you are using xml configuration the Convert.ChangeType is used. Behind the scenes this uses datetime.parse(string, IFormatProvider) where IFormat provider is Thread.CurrentThread.CurrentCulture. So if it is parsing as mm/dd/yyyy it meras that is what your machine is set to as your current culture.

This is actually a problem. I will refactor the code tonight so that the xml configuration uses the same datetime parsing as the attributes.
Sep 27, 2007 at 4:38 PM
SimonCropp,

I am new to the Framework, so please be patient with me.

I am using RequiredDateTimeRuleAttribute to add rule to DOB field with the required format dd/mm/yyyy. How do I enforce this while this field is nullable?

private datetime?_dob;
public datetime? DOB
{
get{return _ dob;}
set{_dob=value;}
}

An example code would be helpful.

tumay
Coordinator
Sep 27, 2007 at 11:39 PM
Tumay

I am having trouble understanding what you are trying to achieve.

When a RequiredRule<T> is applied to a DateTime property it acts as follows
-if the property is a nullable DateTime the when the property is null it is invalid
-if the property is a DateTime then when the property equals DateTime.MinValue it is invalid
-if you set an InitialValue for the rule then when the property equals that InitialValue it is invalid. Null values are ignored if InitalValue is set.

It is the setting of the InitialValue that the aforementioned parsing formats are used.

So as you can see in the below example there are two rules applied to the “Data” property. So “Data” is invalid if it equals null OR it equals “"05 Jan 2006".

 
     //ErrorMessage will be generated for this attribute
        [RequiredDateTimeRule]
        //Defined ErrorMessage and InitialValue
        [RequiredDateTimeRule(InitialValue = "05 Jan 2006",
          ErrorMessage = "05 Jan 2006 is an invalid value.")]
        public DateTime? Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }
 

It does not really make sense to have a “required format” on a DateTime property. DateTimes do not have an internal format that they are stored in. It is only when they are converted from/to a string that this format is important.

Perhaps what you want is a string property where the format is validated to check if it conforms to a specific DateTime format??
Oct 30, 2007 at 2:35 PM
Hi SimonCropp,

I have the following code:

[RequiredDateTimeRule(InitialValue = "01/01/1900",
ErrorMessage = "01/01/1900 is an invalid value.")]
public DateTime DOB
{
get { return _dOB; }
set { _dOB = value; }
}

This is a required field and only accept dates with format dd/mm/yyyy. The code above doesn't work for my requirements.
Can you guide me how to achieve this behavior?

tumay
Coordinator
Nov 6, 2007 at 11:09 AM
Tumay
What exactly is your requirement?
Why doesn’t the above code meet your requirements?
I don’t understand what defining a format has to do with a required field?

If, however, you want to have a required format for a datetime that is being stored as a string I would do it as following

using System;
using System.Collections.Generic;
using System.Globalization;
using NUnit.Framework;
using ValidationFramework;
 
namespace ExampleLibraryCSharp
{
    [TestFixture]
    public class Person
    {
 
        #region Fields
 
        private string dateOfBirth;
 
        #endregion
 
 
        #region Properties
 
 
        [CustomRule("ExampleLibraryCSharp.Person,ExampleLibraryCSharp", "CheckValidDateTimeFormat",
      "Validate based on date time format.", ErrorMessage = "Date format is invalid.")]
        public string DateOfBirth
        {
            get
            {
                return dateOfBirth;
            }
            set
            {
                dateOfBirth = value;
            }
        }
 
 
        #endregion
 
 
        #region Methods
 
        private static void CheckValidDateTimeFormat(object sender, CustomValidationEventArgs e)
        {
            string propertyValue = (string)e.TargetMemberValue;
            DateTime temp;
            if (DateTime.TryParseExact(propertyValue, "dd/MM/yyyy", null, DateTimeStyles.None, out temp))
            {
                e.IsValid = true;
            }
            else
            {
                e.IsValid = false;
            }
        }
 
        #endregion
 
        [Test]
        public void test()
        {
 
            Person person = new Person();
            IList<ValidationResult> properties;
            properties = PropertyValidationManager.ValidateAllProperties(person);
            Assert.AreEqual(1, properties.Count);
 
            person.DateOfBirth = "12/22/2003";
            properties = PropertyValidationManager.ValidateAllProperties(person);
            Assert.AreEqual(1, properties.Count);
 
            person.DateOfBirth = "22/12/2003";
            properties = PropertyValidationManager.ValidateAllProperties(person);
            Assert.AreEqual(0, properties.Count);
        }
    }
}