This is a migrated thread and some comments may be shown as answers.

DatePicker now forces the user to enter slashes

21 Answers 1053 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 26 Jul 2010, 06:50 PM
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?

21 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 1
answered on 27 Jul 2010, 04:16 PM
Really could use an answer on this soon.  Thanks
0
George
Telerik team
answered on 29 Jul 2010, 03:58 PM
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
0
Kevin
Top achievements
Rank 1
answered on 29 Jul 2010, 04:26 PM
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?
0
Kaloyan
Telerik team
answered on 30 Jul 2010, 11:58 AM
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
0
Murray Eaton
Top achievements
Rank 1
answered on 30 Aug 2010, 08:48 PM
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.
0
Kaloyan
Telerik team
answered on 02 Sep 2010, 08:15 AM
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
0
haagel
Top achievements
Rank 1
answered on 30 Nov 2010, 11:32 AM
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.
0
Kaloyan
Telerik team
answered on 03 Dec 2010, 02:50 PM
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
0
Jax
Top achievements
Rank 2
answered on 26 Sep 2011, 10:53 AM
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;
      }
0
Murray
Top achievements
Rank 2
answered on 26 Sep 2011, 04:47 PM
Thanks Jax!

Is this the XAML?

ParseDateTimeValue="DatePicker_ParseDateTimeValue" 
0
Jax
Top achievements
Rank 2
answered on 26 Sep 2011, 04:53 PM
yip - PS: Use CultureInfo.CurrentCulture - not CurrentUICulture.
FxCop recommendation after I posted the sample code.
0
Murray
Top achievements
Rank 2
answered on 26 Sep 2011, 04:58 PM
Can you check my sample? The parsing doesnt seem to work?

http://muzman.net/telerik/DatePickerTest.zip
0
Jax
Top achievements
Rank 2
answered on 26 Sep 2011, 05:03 PM
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

0
Murray
Top achievements
Rank 2
answered on 26 Sep 2011, 05:05 PM
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.
0
Jax
Top achievements
Rank 2
answered on 27 Sep 2011, 07:21 AM
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....
0
Jax
Top achievements
Rank 2
answered on 27 Sep 2011, 11:43 AM
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;
 
 
       }
0
Murray
Top achievements
Rank 2
answered on 27 Sep 2011, 11:43 AM
That would be awesome Jax! Make sure you post your code in the Code Library when you're satisfied.
0
Murray
Top achievements
Rank 2
answered on 27 Sep 2011, 11:46 AM
P.S. Your code above works for me perfectly!
0
Murray
Top achievements
Rank 2
answered on 27 Sep 2011, 11:54 AM
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.
0
Jax
Top achievements
Rank 2
answered on 27 Sep 2011, 11:58 AM
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."
0
Murray
Top achievements
Rank 2
answered on 27 Sep 2011, 12:08 PM
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!
Tags
DatePicker
Asked by
Kevin
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 1
George
Telerik team
Kaloyan
Telerik team
Murray Eaton
Top achievements
Rank 1
haagel
Top achievements
Rank 1
Jax
Top achievements
Rank 2
Murray
Top achievements
Rank 2
Share this question
or