从字符串中提取时间 [英] Extract time from String

查看:439
本文介绍了从字符串中提取时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串,它是cmd中 net time \SERVER_NAME 命令的结果:

I have this string which is a result of net time \SERVER_NAME command in cmd :


\SERVER_NAME的当前时间是2014年3月31日上午9:35:57

Current time at \SERVER_NAME is 3/31/2014 9:35:57 AM

命令已成功完成。

我想提取此字符串中显示的时间(在这种情况下为9:35:37 AM)。
我认为它是使用一些正则表达式完成的。有人可以帮忙吗?

I want to extract the time displayed in this string (9:35:37 AM in this case). I think it is done using some regex. Can anyone help please?

编辑

我要绝对使用正则表达式,此命令不仅仅针对一台服务器运行。

I want absolutly to use regular expressions, This command is not ran against just one server.

编辑2

我最终选择了本文档中介绍的API( http ://msdn.microsoft.com/zh-cn/library/aa370612(v = vs.85).aspx ),由于使用NTP,因此它是可靠的一种。

I finally opted for the API presented in this documentation ( http://msdn.microsoft.com/en-us/library/aa370612(v=vs.85).aspx ), it is apprently a reliable one since it is using NTP.

推荐答案

获取一天中的远程时间...

如果您正在尝试与具有多种区域性的机器进行对话-并且如果您正在运行 的系统的区域性也可能有所不同-我强烈建议您避免使用这种基于文本的方法。

If you're trying to talk to machines with multiple cultures - and if the culture of the system on which you're running may vary as well - I would strongly advise that you avoid this text-based approach.

相反,请考虑使用 NetRemoteTOD 功能。可能有此版本的托管版本,但如果没有,则可以使用P / Invoke进行调用-请参见相关的pinvoke.net条目

Instead, consider using the NetRemoteTOD function. There may be a managed version of this, but if not you can use P/Invoke to call it - see the relevant pinvoke.net entry. This will allow you to get the appropriate value without any text handling, making things much cleaner.

原始答案,这将使您在没有任何文本处理的情况下获得适当的值 。 (可能对其他人有用)

Original answer (may be useful to others)

在工作后,我将使用 DateTime.Parse 而不是正则表达式哪一部分是实际日期/时间。例如:

Well I would use DateTime.Parse rather than a regular expression, after working out which part is the actual date/time. For example:

int startIndex = text.IndexOf(" is ") + 4;
DateTime dateTime = DateTime.Parse(text.Substring(startIndex));

该值将作为 DateTime -然后,您可以根据自己的需要格式化时间部分,例如

That will get the value as a DateTime - you can then format just the time part however you want, e.g.

string time24 = dateTime.ToString("HH:mm:ss"); "09:35:37"
string time12 = dateTime.ToString("h:mm:ss tt"); "9:35:37 AM"

请注意,这很可能对文化敏感;该代码可能无法在其他文化中使用。如果您想使其更可靠,我将停止使用净时间,而是使用API​​与服务器进行通讯-我真的<希望>

Note that this is very likely to be culture-sensitive; the code may well not work in other cultures. If you want to make this more reliable, I would stop using net time and instead use an API to talk to the server - I would really hope there'd be a way of doing this without using text handling...

如果您真的真的只是想原始字符串在日期之后,然后是的,您可以使用正则表达式-但我认为最好将值解析为日期/时间以确保它是真正相关的数据。

If you really, really just want "whatever the part of the original string is after the date" then yes, you could use a regular expression - but I think it would be better to parse the value as a date/time to make sure it's genuinely the relevant data.

这篇关于从字符串中提取时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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