DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Date Regular Expression

  1. #1
    Join Date
    Dec 2004
    Posts
    717

    Date Regular Expression

    I'd like to use one regular expression in my project.
    It can accept all data format such as 1/6/05, 6 Jan, 2005, .....
    Is it possible?
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  2. #2
    Join Date
    Apr 2004
    Posts
    19
    I think what you'd rather do is Parse the input, see if its a Date. If I'm wrong, though, here's how to match mm/dd/yyyy as a RegEXP

    Here's how I would do it:

    Code:
    string ValueToCheck = "January 2nd, 2005";
    try
    {
       DateTime DT = DateTime.Parse(ValueToCheck);
       // if an exception isn't thrown, its a valid date
    }
    catch (FormatException)
    {
      ErrorHandler(ValueToCheck + " is not a valid date");
    }

  3. #3
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    12
    Ah, but here's a serious question...
    If the value received is 01/02/05, then what is the date?

    Or, more correctly, how should you determine the date that is represented by the user-input?

    My point... To Americans, it's January 2nd, 2005. To British, it's February 1st, 2005.
    And... Suppose the date were 01/02/20. Is that 1920 or 2020?

    So, does anybody know the system setting that would let me know how to interpret what the user entered?

  4. #4
    Join Date
    Apr 2004
    Posts
    19
    OK, there are a couple of concepts at work here. Generally, if you set your .NET assembly's culture to be "en-us" then it'll interpret it like Americans expect. That way you could create french, spanish, english assemblies.

    If you need to dynamically parse dates in different languages, then you'd need to have a way of knowing what culture it is (like a drop down list). Then, get the culture for your language (or implement your own System.Globalization.DateTimeFormatInfo object), which you'll pass as the DateTime.Parse constructor

    Example:
    // we need to have imported System.Globalization
    // using System.Globalization;

    // fetch the en-GB culture
    CultureInfo ukCulture = new CultureInfo("en-GB");
    // pass the DateTimeFormat information to DateTime.Parse
    DateTime myDateTime = DateTime.Parse("18/09/2004",ukCulture.DateTimeFormat);

  5. #5
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    12
    Wow...
    Thank you for that.

    But, isn't there something on the System already that "knows" my culture.
    Presuming the operating system was correctly installed in the first place (better in Win-XP, but my memory of Win-NT computers is that they were always US and the dates were ***-up until they were corrected somewhere down in the Control Panel), how can my app figure it out?

    Ie, suppose I want the user to enter their birth-date and then want to work out how many days from then until Christmas.

    An American born on 4th July will enter 07/04/05, but an Englishman will enter 04/07/05, which is entirely different to what would be calculated if they entered 7th April as 07/04/05.

    You see, my "Regional and Language Options" (Windows XP, why does Microsoft always change the name of this feature) tells me that I'm "English (Australia)", and when I search deeper in the features, it tells me that my short-date format is "dd/MM/yyyy"
    It should work for me and tell me that I was born on 4th July when I enter 04/07/05.

    Somehow, when I send my program to my twin in America, it should work when he enters 07/04/05.

  6. #6
    Join Date
    Dec 2004
    Posts
    717
    Hi,
    Actually, I want one regular expression for date validation. The regular exp: could accept all data formats such as dd/mm/yyyy, mmm dd, yyyy.
    Thanks.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  7. #7
    Join Date
    Nov 2004
    Location
    Huddinge, Sweden
    Posts
    283
    Quote Originally Posted by hicksi
    Wow...
    It should work for me and tell me that I was born on 4th July when I enter 04/07/05.

    Somehow, when I send my program to my twin in America, it should work when he enters 07/04/05.
    This is all handled for you. If you do DateTime.Parse() without supplying a curlture, the runtime will use the culture associated with the executing thread which - incidentally - is the same as that which is set in the Control Panel, unless you've explicitly coded to change it.

    Actually, I want one regular expression for date validation. The regular exp: could accept all data formats such as dd/mm/yyyy, mmm dd, yyyy
    There's just no regular expression that can reliably validate dates, so I doubt you'll get one. The best yu can hope for is one that applies a validation mask for a number of common formats, but that'll still not ensure a valid date.

    Rune
    If you hit a brick wall, you didn't jump high enough!

  8. #8
    Join Date
    Dec 2004
    Posts
    717
    There's just no regular expression that can reliably validate dates, so I doubt you'll get one. The best yu can hope for is one that applies a validation mask for a number of common formats, but that'll still not ensure a valid date.
    Yeah. I'm not sure to get one. I think If i want this one, you must write the combination of all format for data validation.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  9. #9
    Join Date
    Jun 2005
    Posts
    1
    You can visit this page and see if maybe you can combine the expressions they have for the different formats......
    http://academic1.bellevue.edu:8080/pval2.jsp

  10. #10
    Join Date
    Dec 2004
    Posts
    717
    thanks
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center