1

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

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

The command completed successfully.

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?

Edit

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

Edit 2

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.

Hamdi Baligh
  • 874
  • 1
  • 11
  • 30
  • As @JonSkeet mentions in his answer, a better solution would be to query the remote server's time via a dedicated API, such as NTP: http://stackoverflow.com/questions/1008111/get-the-exact-time-for-a-remote-server – Avner Shahar-Kashtan Mar 31 '14 at 08:55

2 Answers2

5

Obtaining a remote time of day...

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.

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)

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));

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"

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Don't forget to drop the rest of the string after the date-time part. – Avner Shahar-Kashtan Mar 31 '14 at 08:48
  • I think OP wants the time only. So you need to change the IndexOf as presently it will give date as well! – Rahul Tripathi Mar 31 '14 at 08:48
  • Why didn't you split the string instead of substring it? – Rand Random Mar 31 '14 at 08:51
  • 1
    The command can be ran under different servers with different cultures, so the output can be different, if this method is case sensitive so it's not the best choici for me. This is why I want to use regx in order to have the first occurence of time in a string. – Hamdi Baligh Mar 31 '14 at 08:51
  • @HamdiBaligh: Well given that the time part could appear using different separators, in different positions, with different cultures etc I think you're really on a hiding to nothing. Do you absolutely *have* to use `net time`? – Jon Skeet Mar 31 '14 at 08:53
  • @RandRandom: Splitting would be fine too, yes. It just doesn't feel as natural to me in this case. – Jon Skeet Mar 31 '14 at 08:53
  • Yes, I want to have the time from a remote server, so net time is the first choice – Hamdi Baligh Mar 31 '14 at 08:54
  • 1
    @HamdiBaligh: Why not use the protocol *used* by `net time` instead? This is like trying to access mail by logging into a web mail account and screen-scraping, instead of using a mail-based API... – Jon Skeet Mar 31 '14 at 08:55
  • @HamdiBaligh: In particular, consider http://msdn.microsoft.com/en-us/library/aa370612(v=vs.85).aspx for example. – Jon Skeet Mar 31 '14 at 08:59
  • Yes, thank you for that, it is much better than using net time, I feel confortable with it. thanks a lot – Hamdi Baligh Mar 31 '14 at 09:00
0

The simplest regex to only match the time would be something like:

\d\d?\:\d\d\:\d\d\s?(A|P)M

Is that what you're after?

Rick Hoving
  • 3,585
  • 3
  • 29
  • 49
Katia
  • 26
  • 1