C# 正则表达式,格式化时间

发布于 2024-09-30 14:32:13 字数 790 浏览 0 评论 0原文

我正在制作一个考勤卡程序,用户在其中输入开始时间、结束时间和项目名称。他们可以单击“添加”,并将输入的详细信息添加到列表框中。

我不允许添加数据,除非开始时间和结束时间的格式如下: 08:14am 不过,每次都必须完全一样地输入,这很烦人,所以我决定通过正则表达式自动格式化。因此,如果您输入 8:14,它会将其更改为 08:14am。如何通过 Regex.Replace() 完成此操作?

如果您还有其他方法,请随时列出。

编辑1:我还考虑了其​​他替代品;例如,814 转到 08:14am8 转到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

潜移默化 2024-10-07 14:32:13

你看过DateTime.Parse吗?它接受 "08:14am""8:14"[1] 和其他变体,并返回设置为 8:14 的 DateTime是在今天的日期。

[1] 至少在英国文化中 - 考虑提供 CultureInfo 参数,具体取决于您是要关注用户的本地格式首选项,还是采用固定格式。

编辑我还考虑了其​​他替代品;例如 814 表示上午 08:14,8 表示上午 08:00

看一下 DateTime.ParseExact:您可以提供一组有效的时间格式(例如“hh:mm”、“hmm”、“h”等)。 )

Have you looked at DateTime.Parse? It accepts "08:14am", "8:14"[1] and other variants, and returns a DateTime 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.

EDIT I also have other replacements in mind; for example 814 goes to 08:14am, and 8 goes to 08:00am

Take a look at DateTime.ParseExact: you can provide an array of valid time formats (such as "hh:mm", "hmm", "h" etc.)

金橙橙 2024-10-07 14:32:13

从完全不同的方向来看,在输入上使用掩码怎么样?

例如,有许多 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.

来日方长 2024-10-07 14:32:13

您考虑过使用 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

夜司空 2024-10-07 14:32:13

我认为正则表达式对此太过分了。为什么不简单地在末尾附加“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.

墟烟 2024-10-07 14:32:13

如果可能的话,我建议不要使用正则表达式(正如其他人所说,这似乎有点矫枉过正)并尝试使用日期时间格式。请参阅此处: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文