DatePicker now forces the user to enter slashes

22 posts, 0 answers
  1. Kevin
    Kevin avatar
    31 posts
    Member since:
    Jan 2009

    Posted 26 Jul 2010 Link to this post

    In a previous release the user could simply type 072610 and achieve the 07/26/2010 date.  Now the user must also type in the slashes.  Was this change permanent or can it be changed by a property?
  2. Kevin
    Kevin avatar
    31 posts
    Member since:
    Jan 2009

    Posted 27 Jul 2010 Link to this post

    Really could use an answer on this soon.  Thanks
  3. DevCraft banner
  4. George
    Admin
    George avatar
    1332 posts

    Posted 29 Jul 2010 Link to this post

    Hi Kevin,

    Thank you for contacting us.

    We made some changes in the Parser in date/time pickers and that it is what causes the described behavior. We are sorry for any inconvenience that this causes to you.  However when we did the changes we decided not to support this scenario for this moment.

    You could workaround it by implementing your own parser that suits your needs. You need to handle ParseDateTimeValue event and to parse the input string to DateTime value. Attached you can find a sample that I made you that illustrates how to accomplish this.

    Please do not hesitate to contact us if you require any further information.

    Regards,
    George
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Kevin
    Kevin avatar
    31 posts
    Member since:
    Jan 2009

    Posted 29 Jul 2010 Link to this post

    If I set the args.Result to the value I want the property binding still does not refresh.  I've tried setting args.Handle = true, but that did not help.

    What do I need to call in order to get the control to recognize the value?
  6. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 30 Jul 2010 Link to this post

    Hello Kevin,

    You need to set args.IsParsingSuccessful = true, after setting the args.Result to your parsed value:

    private void RadDatePicker_ParseDateTimeValue(object sender, Telerik.Windows.Controls.ParseDateTimeEventArgs args)
           {
               if (!args.IsParsingSuccessful)
               {
                   var time = new  DateTime();
                   // here you can put your logic
                   // finally set the parsed time to Result
                   args.Result = time;
                   args.IsParsingSuccessful = true;
               }
           }


    Regards,
    Kaloyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Murray Eaton
    Murray Eaton avatar
    3 posts
    Member since:
    May 2010

    Posted 30 Aug 2010 Link to this post

    private void RadDatePicker_ParseDateTimeValue(object sender, Telerik.Windows.Controls.ParseDateTimeEventArgs args)
            {
                if (!args.IsParsingSuccessful)
                {
                    DateTime time = new DateTime();
      
                    // here you can put your logic
      
                    // finally set the parsed time to Result
                    args.Result = time;
                }
            }

    Could you show us the complete parser to perform the "/" masking?

    Data entry clerks using this component would complain due to the requirement of adding a "/" seperator manually.
  8. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 02 Sep 2010 Link to this post

    Hello Murray Eaton,

    Currently we don't have such a parser. If the requirement is mandatory you may use the RadMaskedTextBox. Check our online example for further reference.

    Greetings,
    Kaloyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 30 Nov 2010 Link to this post

    I want my users to be able to input dates in the format "20101030", i.e. without dashes.

    Using your example I'm able to do this parsing. However, the behavior is not satisfactory...

    The parsing event occurs when the DatePicker loses focus, but there also seem to be some kind of continious parsing running that indicates whether the text in the DatePicker can be parsed and what the result will be (shown in a tooltip above the DatePicker). If the user types "20100814" this will be successfully parsed with my own parsing, but the tooltip still shows "Error".

    Is it possible to hook in to this "pre-parsing" as well?

    By the way... I also need to change the text "Error" shown in the tooltip since all of my application is in swedish, so any suggestions on how to do that is welcome.
  10. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 03 Dec 2010 Link to this post

    Hi David Haglund,

    The error tooltip content is localized by default. It's value equals to "error" and also the key stays the same. Creating a custom localization resource file will help you translate it to your language. You can set the property via the ErrorTooltipContent property. As about the parser - we have logged the requirenment we are doing the necessary to implement it for the next iteration of the product. As for now try using the ParseDateTimeValue, where you can set a custom parser value in case of invalid entered one.

    Regards,
    Kaloyan
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  11. Jax
    Jax avatar
    30 posts
    Member since:
    Sep 2009

    Posted 26 Sep 2011 Link to this post

    For those looking for some sample code for entering the date without separators:
    private void DatePicker_ParseDateTimeValue(object sender, ParseDateTimeEventArgs args)
          {
              if (!args.IsParsingSuccessful) //ie: Can't parse the date the user has entered
              {
                  DateTime time;
     
                  if (TryParseDate(args.TextToParse, out time))
                  {
                      args.Result = time;
     
                      args.IsParsingSuccessful = true;
                  }
              }
          }      
     
          private bool TryParseDate(string date, out DateTime dateTime)
          {
              bool parsedSuccess = false;
              dateTime = DateTime.Now;
     
              int isNumber;
              //1) Determine if no separators were specified
              if (int.TryParse(date, NumberStyles.Integer, CultureInfo.CurrentUICulture, out isNumber)) //ie: If the user typed only numerics, leaving out any separators
              {
                  //2) Determine if the length without separators is correct               
                  string currentDateFormat = DateTimeFormatInfo.GetInstance(CultureInfo.CurrentUICulture).ShortDatePattern; //get current short date pattern which includes separator
                  string todayInCurrentDate = DateTime.Now.ToShortDateString();
                  string currentDateFormatWithoutSeparator = new string(todayInCurrentDate.Where(ch => char.IsDigit(ch)).ToArray());
                  if (date.Length == currentDateFormatWithoutSeparator.Length)
                  {
                      //3) Determine what the separator is
                      char separator = todayInCurrentDate.Where(ch => !char.IsDigit(ch)).First();                     
     
                      //4) Insert the separator into the date string
                      List<int> separatorIndexes = new List<int>();
                      int currentIndex = currentDateFormat.IndexOf(separator, 0, currentDateFormat.Length);
                      while (currentIndex > 0)
                      {
                          separatorIndexes.Add(currentIndex);
                          currentIndex = currentDateFormat.IndexOf(separator, currentIndex + 1, currentDateFormat.Length - currentIndex - 1) ;
                      }
     
                      foreach (int separatorIndex in separatorIndexes)
                      {
                          date = date.Insert(separatorIndex, new string(separator, 1));
                      }
     
                      //5) Parse the new date string to check validity
                      DateTime parseDate;
                      if (DateTime.TryParse(date, CultureInfo.CurrentUICulture, DateTimeStyles.AssumeLocal, out parseDate))
                      {
                          dateTime = parseDate;
                          parsedSuccess = true;
                      }
                  }
     
               
              }
     
     
              return parsedSuccess;
          }
  12. Murray
    Murray avatar
    121 posts
    Member since:
    Aug 2010

    Posted 26 Sep 2011 Link to this post

    Thanks Jax!

    Is this the XAML?

    ParseDateTimeValue="DatePicker_ParseDateTimeValue" 
  13. Jax
    Jax avatar
    30 posts
    Member since:
    Sep 2009

    Posted 26 Sep 2011 Link to this post

    yip - PS: Use CultureInfo.CurrentCulture - not CurrentUICulture.
    FxCop recommendation after I posted the sample code.
  14. Murray
    Murray avatar
    121 posts
    Member since:
    Aug 2010

    Posted 26 Sep 2011 Link to this post

    Can you check my sample? The parsing doesnt seem to work?

    http://muzman.net/telerik/DatePickerTest.zip
  15. Jax
    Jax avatar
    30 posts
    Member since:
    Sep 2009

    Posted 26 Sep 2011 Link to this post

    Works fine on my machine...

    So if the short date time format is "YYYY/MM/DD"
    .. then you enter "20101226" - the tooltip should resolve to 2010/12/26

  16. Murray
    Murray avatar
    121 posts
    Member since:
    Aug 2010

    Posted 26 Sep 2011 Link to this post

    What location are you? I am in a US timezone so our native format is mm/dd/yyyy

    Did you test from my sample project? I tried adding Culture="en-ZA" which gives me the format you mentioned, but still no luck.
  17. Jax
    Jax avatar
    30 posts
    Member since:
    Sep 2009

    Posted 27 Sep 2011 Link to this post

    Busy running through some other timezone examples - will post here if I find something...

    Edit:
    I see the issue: My code was assuming a fixed length dateTime format - so it would always know where to inject the separators.
    Some cultures though (like the US) use only single digit for day and month values (ie: day is written as "1" instead if "01"

    I'll update here if/when I find a solution....
  18. Jax
    Jax avatar
    30 posts
    Member since:
    Sep 2009

    Posted 27 Sep 2011 Link to this post

    right - updated the code:
    If a region is using a format that allows single-digit days or months, then unfortunately the user MUST specify 2 digits for month and day, else it's impossible to discern the date: eg: 1122010 - is that 1 Dec, or 11 Feb 2010? It must be specified: 01122010 or 11012010

    public static bool TryParseNumberAsDate(string date, out DateTime dateTime)
           {
               bool parsedSuccess = false;
               dateTime = DateTime.Now;
     
               int isNumber;
               //1) Determine if no separators were specified
               if (int.TryParse(date, NumberStyles.Integer, CultureInfo.CurrentCulture, out isNumber)) //ie: If the user typed only numerics, leaving out any separators
               {
                   //2) Determine if the length without separators is correct                               
                   string longestDate = new DateTime(1977, 12, 20).ToShortDateString();
                   string longestDateFormatWithoutSeparator = new string(longestDate.Where(ch => char.IsDigit(ch)).ToArray());
                   if (date.Length == longestDateFormatWithoutSeparator.Length) //NOTE: Some cultures write "DD" "01" as "1" - force them to type "01"
                   {
                       //3) Determine what the separator is
                       char separator = longestDate.Where(ch => !char.IsDigit(ch)).First();
     
                       //4) Insert the separator into the date string
                       List<int> separatorIndexes = new List<int>();
                       int currentIndex = longestDate.IndexOf(separator, 0, longestDate.Length);
                       while (currentIndex > 0)
                       {                       
                           separatorIndexes.Add(currentIndex);
                           currentIndex = longestDate.IndexOf(separator, currentIndex + 1, longestDate.Length - currentIndex - 1);
                       }
     
                       foreach (int separatorIndex in separatorIndexes)
                       {
                           date = date.Insert(separatorIndex, new string(separator, 1));
                       }
     
     
                       //5) Parse the new date string to check validity
                       DateTime parseDate;
                       if (DateTime.TryParse(date, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out parseDate))
                       {
                           dateTime = parseDate;
                           parsedSuccess = true;
                       }
                   }
     
               }
     
     
               return parsedSuccess;
     
     
           }
  19. Murray
    Murray avatar
    121 posts
    Member since:
    Aug 2010

    Posted 27 Sep 2011 Link to this post

    That would be awesome Jax! Make sure you post your code in the Code Library when you're satisfied.
  20. Murray
    Murray avatar
    121 posts
    Member since:
    Aug 2010

    Posted 27 Sep 2011 Link to this post

    P.S. Your code above works for me perfectly!
  21. Murray
    Murray avatar
    121 posts
    Member since:
    Aug 2010

    Posted 27 Sep 2011 Link to this post

    To make this fully robust, how would it handle "any" culture? Your update is perfect for US but it would need to be able to parse any users culture if used for everyones production applications.
  22. Jax
    Jax avatar
    30 posts
    Member since:
    Sep 2009

    Posted 27 Sep 2011 Link to this post

    It should take any culture into account - I've tested it for swedish / german / south africa, and it works for all those scenarios.
    In the code, the ",ToShortDateString()" returns the date in a format that is in the current culture used by the browser.

    Let me know if this is not accurate... our LOB app ships world-wide, so it has to work for all the culture scenarios.

    From MSDN:
    "The value of the current DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread culture."
  23. Murray
    Murray avatar
    121 posts
    Member since:
    Aug 2010

    Posted 27 Sep 2011 Link to this post

    Ahh. I needed to set my computer region format to the desired country first (not just specify a region in XAML).

    Works for AU, Afrikaans, Arabic and German (Austrian) also.

    I think we have a winner!
Back to Top
DevCraft banner