Help:Datetime format string
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Help:Datetime format string

  1. #1
    Join Date
    Apr 2004
    Posts
    123

    Help:Datetime format string

    Hi,

    Iím looking for a small help for forming the correct datetime format string for validating an user input.

    Case 1:
    User can enter 2008-11-08 or 11 nov 08 or 08 11 nov with spaces between dd mm and yyyy.If he enters any of these strings I should recognize it as a valid input and do an associated action.

    Case 2:
    Also there is another filed wherin he can enter nov 08,or 08 nov or 11 08 or 08 11 with or without spaces between them and I should recognize the month and the year.Please note that he wont enter the day, if he enters the day then I should take it as the first case and if he enters only month and year it should be the second case.

    Also this should be culture independent as it can be used across globally.What I was planning to do was to parse the user input using datetime.tryparse and datetime .tryparseexact,but unfortunately Iím not able to form the exact expression.Can you please help me in this?

    Thanks in advance,
    mmx

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Help:Datetime format string

    it's going to have to be custom.

    the DateTime type can accept a great number of string values and convert them into a date.

    However, what you want is outside of it's ability. If you try to convert "nov 11" to a datetime, it will assume that the "11" is the day, but you want it to assume it's the year.

    also, if you give it "1112"(same as "nov 12"), it will assume that "1112" is a year, so it will give you january 1, 1112.

    so basically, a numeric date with no space is going to cause you a problem, and when you give it the month and year, that will also give you a problem.

    also, it can't be "culture independant". Since different cultures have different display types, how is the application going to know whether to display "nov 20 09" as 11-20-09 or 20-11-09? YOU are going to have to tell it the culture.

  3. #3
    Join Date
    Apr 2004
    Posts
    123

    Re: Help:Datetime format string

    Agreed with the space issue.I'm going to go back and convince them that space is going to be mandatory between mm dd and yy.

    Is there a single format string which can satisfy the following scenarios??

    Nov 08
    November 08
    09 08
    09 2008
    Nov 2008
    November 2008

    i.e when i pass the fomat to Datetime.tryParseExact it should only return true if any of the sequence comes.Also how do i incorporate culture info along with this?


    Thanks,
    mmx

  4. #4
    Join Date
    Apr 2004
    Posts
    123

    Re: Help:Datetime format string

    I almost got it,but it looks ugly..is there a single string format which can satisfy my case?

    Code:
    if (DateTime.TryParseExact(enteredText,
                            new string[] {
                                "M y",
                                "MM y",
                                "MMM y",
                                "MMMM y",                         
                                "M yyyy", 
                                "MM yyyy",
                                "MMM yyyy",
                                "MMMM yyyy", 
                                "M yy",
                                "MM yy",
                                "MMM yy",
                                "MMMM yy",
                                "yyyy M", 
                                "yyyy MM", 
                                "yyyy MMM",
                                "yyyy MMMM",                            
                                "yy M", 
                                "yy MM", 
                                "yy MMM",
                                "yy MMMM",  
                                "y M", 
                                "y MM", 
                                "y MMM",
                                "y MMMM"  
                            },
    
                            DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowWhiteSpaces, out tempDateB) == true)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center