C# 正则表达式,格式化时间
我正在制作一个考勤卡程序,用户在其中输入开始时间、结束时间和项目名称。他们可以单击“添加”,并将输入的详细信息添加到列表框中。
我不允许添加数据,除非开始时间和结束时间的格式如下: 08:14am
不过,每次都必须完全一样地输入,这很烦人,所以我决定通过正则表达式自动格式化。因此,如果您输入 8:14
,它会将其更改为 08:14am
。如何通过 Regex.Replace()
完成此操作?
如果您还有其他方法,请随时列出。
编辑1:我还考虑了其他替代品;例如,814
转到 08:14am
,8
转到 08:00am
编辑2:所以现在我正在使用这段代码:
string[] formats = { "h:mm", "h", "hh", "hmm", "hhmm", "h:mmtt", "htt", "hhtt" };
DateTime dt = DateTime.ParseExact(t.Text, formats, new CultureInfo("en-US"), DateTimeStyles.None);
t.Text = dt.ToString("hh:mmtt").ToLower();
它替换了一些内容,例如h:mm
,但不替换其他内容,例如h
。
I'm making a timecard program, where the user enters the start time, end time, and project name. They can click "Add" and it adds the entered details to a listbox.
I don't allow the data to be added unless the start time and end time are formatted like this: 08:14am
It gets annoying, though, having to enter it exactly like that each time, so I decided to, via regexes, have it automatically format. So if you enter 8:14
, it will change it to 08:14am
. How can I accomplish this via Regex.Replace()
?
If you have any other methods, don't hesitate to list them.
Edit 1: I also have other replacements in mind; for example 814
goes to 08:14am
, and 8
goes to 08:00am
Edit 2: So Now I'm using this code:
string[] formats = { "h:mm", "h", "hh", "hmm", "hhmm", "h:mmtt", "htt", "hhtt" };
DateTime dt = DateTime.ParseExact(t.Text, formats, new CultureInfo("en-US"), DateTimeStyles.None);
t.Text = dt.ToString("hh:mmtt").ToLower();
And it replaces some things, like h:mm
but not others like h
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你看过
DateTime.Parse
吗?它接受"08:14am"
、"8:14"
[1] 和其他变体,并返回设置为 8:14 的DateTime
是在今天的日期。[1] 至少在英国文化中 - 考虑提供
CultureInfo
参数,具体取决于您是要关注用户的本地格式首选项,还是采用固定格式。看一下
DateTime.ParseExact
:您可以提供一组有效的时间格式(例如“hh:mm”、“hmm”、“h”等)。 )Have you looked at
DateTime.Parse
? It accepts"08:14am"
,"8:14"
[1] and other variants, and returns aDateTime
set to 8:14 am on today's date.[1] In the UK culture at least -- consider providing a
CultureInfo
parameter depending on whether you want to pay attention to the user's local format preferences, or adopt a fixed format.Take a look at
DateTime.ParseExact
: you can provide an array of valid time formats (such as "hh:mm", "hmm","h"etc.)从完全不同的方向来看,在输入上使用掩码怎么样?
例如,有许多 JavaScript 和 jQuery 工具可用于执行此操作。
使用此方法可以保留用户对输入的部分控制,并让用户看到他们的输入。
Coming at it from a completely different direction, how about using a mask on the input?
For example, there are a number of JavaScript and jQuery tools available to do this.
Using this approach retains some user control over the input, and lets the user see their input.
您考虑过使用 DateTimePicker 吗?让用户写下时间看起来更麻烦。
请参阅 http://msdn.microsoft.com/en -us/library/system.windows.forms.datetimepicker.aspx
have you considered using a DateTimePicker? Making the user write the time looks like more of a hassle.
See e.g. http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.aspx
我认为正则表达式对此太过分了。为什么不简单地在末尾附加“am”,以防万一您在方法中使用字符串时已经添加了 2 行代码。
I think regex are overkill for this. Why not simply append the "am" at the end, in case nto already added is a 2 line code in case you are working with strings in the method.
如果可能的话,我建议不要使用正则表达式(正如其他人所说,这似乎有点矫枉过正)并尝试使用日期时间格式。请参阅此处: http://msdn.microsoft.com/en-us/library/ 8kb3ddd4.aspx
“HH”格式标记会将小时格式化为从 00 到 23 的 24 小时制时间。传统上,12 小时制时间不会向左补零,但 24 小时制时间是(至少我经常看到它们),可能是为了避免 1800 和 0800 之间的混淆。或者,如果您不需要 24 小时时间,您可以在字符串的小时部分左侧填充 0 到 2 位数字。
编辑:
根据您的新要求,我会说编写一个简单的解析器,让用户输入表示“08:14 AM”的“814”,并利用时间格式化功能进行显示。
I would recommend against using regular expressions if possible (as others have said, it seems like overkill) and try using datetime formats. See here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
The "HH" format tag will format the hour as a 24-hour time from 00 to 23. Traditionally, 12 hour times are not zero-padded to the left, but 24 hour times are (at least as often as I see them), probably to avoid confusion between 1800 and 0800 . Alternatively, if you don't want 24 hour times, you could probably just left-pad the hour part of the string with a zero to up to 2 digits.
EDIT:
Based on your new requirements, I'd say write a simple parser to let users input "814" meaning "08:14 AM" and make use of the time-formatting functionality for display.