Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Silverlight > DatePicker > DatePicker now forces the user to enter slashes

Not answered DatePicker now forces the user to enter slashes

Feed from this thread
  • Kevin avatar

    Posted on Jul 26, 2010 (permalink)

    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?

    Reply

  • Kevin avatar

    Posted on Jul 27, 2010 (permalink)

    Really could use an answer on this soon.  Thanks

    Reply

  • George George admin's avatar

    Posted on Jul 29, 2010 (permalink)

    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
    Attached files

    Reply

  • Kevin avatar

    Posted on Jul 29, 2010 (permalink)

    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?

    Reply

  • Kaloyan Kaloyan admin's avatar

    Posted on Jul 30, 2010 (permalink)

    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

    Reply

  • Murray Eaton avatar

    Posted on Aug 30, 2010 (permalink)

    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.

    Reply

  • Kaloyan Kaloyan admin's avatar

    Posted on Sep 2, 2010 (permalink)

    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

    Reply

  • Posted on Nov 30, 2010 (permalink)

    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.

    Reply

  • Kaloyan Kaloyan admin's avatar

    Posted on Dec 3, 2010 (permalink)

    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

    Reply

  • Jax avatar

    Posted on Sep 26, 2011 (permalink)

    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;
          }

    Reply

  • Posted on Sep 26, 2011 (permalink)

    Thanks Jax!

    Is this the XAML?

    ParseDateTimeValue="DatePicker_ParseDateTimeValue" 

    Reply

  • Jax avatar

    Posted on Sep 26, 2011 (permalink)

    yip - PS: Use CultureInfo.CurrentCulture - not CurrentUICulture.
    FxCop recommendation after I posted the sample code.

    Reply

  • Posted on Sep 26, 2011 (permalink)

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

    http://muzman.net/telerik/DatePickerTest.zip

    Reply

  • Jax avatar

    Posted on Sep 26, 2011 (permalink)

    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

    Reply

  • Posted on Sep 26, 2011 (permalink)

    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.

    Reply

  • Jax avatar

    Posted on Sep 27, 2011 (permalink)

    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....

    Reply

  • Jax avatar

    Posted on Sep 27, 2011 (permalink)

    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;
     
     
           }

    Reply

  • Posted on Sep 27, 2011 (permalink)

    That would be awesome Jax! Make sure you post your code in the Code Library when you're satisfied.

    Reply

  • Posted on Sep 27, 2011 (permalink)

    P.S. Your code above works for me perfectly!

    Reply

  • Posted on Sep 27, 2011 (permalink)

    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.

    Reply

  • Jax avatar

    Posted on Sep 27, 2011 (permalink)

    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."

    Reply

  • Posted on Sep 27, 2011 (permalink)

    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!

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Silverlight > DatePicker > DatePicker now forces the user to enter slashes
Related resources for "DatePicker now forces the user to enter slashes"

Silverlight DatePicker Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]