英文:
In Canvas Apps can I get a date picker to accept dates written like this 31.01.2001, 30/01/2001, 30-01-2001
问题
我收到了来自我的客户的请求,要求“改进”在DatePicker中键入日期的用户体验,支持多种不同的分隔符。用户故事如下:
- 作为用户,我希望能够通过键入或选择输入日期。如果我键入,我希望能够使用我在编写日期时通常使用的字符,以便我可以轻松地将日期添加到表单中。
这是一个较晚提出的需求,表单已经构建好,并且有几个日期选择器运行良好,所以我希望有一些代码可以用于DatePicker属性,将任何分隔字符替换为斜杠,以便我不必重新格式化所有的日期数据。
键入的原因是需要输入的日期有时可以追溯到许多年前,使用日期选择器的日历部分可能需要多次点击。
幸运的是,日期将始终以英国格式“dd/mm/yyyy”输入。
客户表示我们需要适应以下日期分隔符(./-):
- 31/01/2015
- 31-01-2015
- 31.01.2015
我已经与客户进行了一些关于这个问题的讨论,因为这是一个真正的边缘情况,但他们坚持希望这个功能能够正常工作。
在格式和DefaultDate属性中:
Text(
Substitute(dteDeferredStart.SelectedDate, "-", "/"),
"dd/mm/yyyy"
)
在DefaultDate属性中:
Text(
Substitute(Parent.Default, "-", "/"),
"dd/mm/yyyy"
)
如果我在属性中使用以上任一段代码,当我在日期选择器中键入17-05-2023时,输出的SelectedDate为空。
现在开始头发都快拔光了。
与这些日期相关的有很多验证代码,如果我必须更改日期选择器之外的任何内容,这将是一个大的重写。
希望你们能帮我解决这个问题。
英文:
I've had a request from my client to "improve" the User Experience of typing dates into the DatePicker with a variety of different delimiters. Their user story is:
- As a user I want to be able to enter a date by typing or picking. If I type I want to be able to use the character that I would normally use when writing a date so that I can easily add dates to a form
This has come in as a late requirement, the forms are built and there's several datepickers working well so I was hoping there was some code that can be used on a datepicker property that would replace any separating characters with a slash so that I don't have to reformat all my date data.
The reason typing is needed is that the dates that need to be entered can sometimes be many years in the past which would take a number of clicks to input with the calendar part of the datepicker.
Luckily the dates will always be entered in UK format "dd/mm/yyyy".
The client has said that we need to accommodate the following date separators (./-):
- 31/01/2015
- 31-01-2015
- 31.01.2015
I've had some conversations about this with the client as it's a real edge case but they are adamant that they would like to have this working.
In the Format and DefaultDate property:
Text(
Substitute(dteDeferredStart.SelectedDate,"-","/"),
"dd/mm/yyyy"
)
In the DefaultDate property
Text(
Substitute(Parent.Default,"-","/"),
"dd/mm/yyyy"
)
With either of these bits of code in the property if I type 17-05-2023 into the datepicker the output SelectedDate is Blank.
Starting to tear my hair out now.
There's a whole lot of validation code linked up with these dates so it's going to be a big rewrite if I have to change anything outside of the datepicker.
Hoping you wonderful people will be able to enlighten me.
答案1
得分: 0
我认为这是Power Apps中正则表达式的一个很好的用例。我建议你首先阅读Microsoft的文档。
在你的特定情况下,你应该在TextInput控件的OnChange属性中使用Match()
函数,并使用UpdateContext()将解析的日期保存在本地变量中。然后,你可以在DatePicker的Default属性中使用这个本地变量,使用Reset()来反映在TextInput控件中输入日期。你还应该将TextInput的DelayOutput设置为true,以便逻辑不会在每次按键时都运行。
正则表达式模式应该类似于这样:
(\d{1,2}).(\d{1,2}).(\d{4})
这将返回一个包含3行(日、月、年)的表格。好处是用户可以使用任何分隔符。在Date()
函数内部使用First()
、Last()
、FirstN()
和LastN()
的组合来获取日期类型。
这是我在TextInput.OnChange中测试的内容:
With({ matches: First(MatchAll(Self.Text,"(\d{1,2}).(\d{1,2}).(\d{4})",MatchOptions.Contains)).SubMatches },
Set(myDate,Date(
Value(Last(matches).Value),
Value(Last(FirstN(matches,2)).Value),
Value(First(matches).Value)
))
);
英文:
I think this is a good use case for Regular Expressions in Power Apps. I recommend you read documentation from Microsoft first.
In your specific case you should use the Match()
function in a TextInput control's OnChange property and UpdateContext() to save the parsed date in a local variable. You can then use the local variable in the Default property of the DatePicker and Reset() to reflect the inputting of dates in the TextInput control. You should also set DelayOutput to true for the TextInput so that the logic doesn't run for every keystroke.
The RegEx pattern should look something like this:
(\d{1,2}).(\d{1,2}).(\d{4})
This will return a table of 3 rows (day,month,year). The benefit is that the user can use any of the separators. Use combinations of First()
, Last()
, FirstN()
and LastN()
inside a Date()
function to get a Date type.
Here's what I tested with in the TextInput.OnChange:
With({ matches: First(MatchAll(Self.Text,"(\d{1,2}).(\d{1,2}).(\d{4})",MatchOptions.Contains)).SubMatches },
Set(myDate,Date(
Value(Last(matches).Value),
Value(Last(FirstN(matches,2)).Value),
Value(First(matches).Value)
))
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论