日期选择器并允许多种输入格式 [英] Datepicker and allowing multiple input formats

查看:114
本文介绍了日期选择器并允许多种输入格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期选择器,我希望为用户提供多种格式。例如,对于日期 2014年1月5日,我要允许以下格式:

I've got a Datepicker and I'd like to allow multiple formats for users. For example, for the date January 5th 2014 I want to allow the following formats:


  • 05-01-2014: dd-MM-yyyy

  • 2014年5月5日: d -MM-yyyy

  • 5-1-2014: dM-yyyy

  • 5-01-14: d-MM-yy

  • 5-1-14: dM-yy

  • 2014年5月1日: dd-M-yyyy

  • 05-1-14: dd-M-yy

  • 05-01-14: dd-MM-yy

  • 05012014: ddMMyyyy

  • 050114: ddMMyy

  • 512014: dMyyyy

  • 5114: dMyy

  • 05-01-2014 : dd-MM-yyyy
  • 5-01-2014 : d-MM-yyyy
  • 5-1-2014 : d-M-yyyy
  • 5-01-14 : d-MM-yy
  • 5-1-14 : d-M-yy
  • 05-1-2014 : dd-M-yyyy
  • 05-1-14 : dd-M-yy
  • 05-01-14 : dd-MM-yy
  • 05012014 : ddMMyyyy
  • 050114 : ddMMyy
  • 512014 : dMyyyy
  • 5114 : dMyy

我确实创建了以下测试转换器:

I did create the following test-converter:

public class DatepickerConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return null;

        return ((DateTime)value).ToString("dd-MM-yyyy", CultureInfo.InvariantCulture);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (string.IsNullOrWhiteSpace(value as string)) return null;

        DateTime dt = DateTime.MinValue;
        string[] checks = {
                            "dd-MM-yyyy",
                            "d-MM-yyyy",
                            "d-MM-yy",
                            "d-M-yyyy",
                            "d-M-yy",
                            "dd-M-yyyy",
                            "dd-M-yy",
                            "d-M-yyyy",
                            "d-M-yy",
                            "ddMMyyyy",
                            "ddMMyy",
                            //"dMyyyy",
                            //"dMyy",
                        };
        for(int i = 0; i < checks.Length; i++)
        {
            try
            {
                dt = DateTime.ParseExact(value as string, checks[i], CultureInfo.InvariantCulture);
                if (dt != null) break;
            }
            catch (Exception){ }
        }

        return dt as DateTime?;
    }
}

但是我有一个问题,一个担忧。

I have however one problem, and one concern.

问题是我从未将转换器分配给DatePicker。我已经在Google上进行了搜索,发现了这个stackoverflow答案,但是它似乎并没有在调试模式下进入转换器。 FIXED

The problem is that I've never assigned a converter to a DatePicker. I've googled it and found this stackoverflow answer, but it doesn't seem to go to the converter in debug mode.. FIXED

我担心的是,每次用户输入时都使用此for循环有些事会适得其反。我知道我可以添加一个Property-Delay,以便当用户输入整个日期时可以进行这种格式化,而不是按照用户输入的每个字符进行格式化,但是仍然没有一种更有效/更易读的解决方案而不是一个个地检查所有格式?

And my concern is that using this for-loop every time the user inputs something is a bit performance-counterproductive. I know I can add a Property-Delay so I can do this formatting when the User has put in the entire date, instead of doing this per character the user puts in, but still, isn't there a more efficient / more readable solution than checking all formats one by one?

推荐答案

抛出异常每一步都会花费很多性能。摆脱 try {} catch {} ,摆脱 for循环,并使用用户友好的 TryParseExact 。它将接受您的支票 数组变量。

Throwing exceptions each step costs a lot performance. Get rid of try{} catch{}, the for loop and use the very user-friendly TryParseExact. It will accept your 'checks' array variable.

if(DateTime.TryParseExact(value as string, checks, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
    // success
}

这篇关于日期选择器并允许多种输入格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆