1

I got acces to a XML file with the following data:

<VertrekTijd>2014-05-26T11:15:00+0200</VertrekTijd>

I use the following code to read this data:

case "VertrekTijd": lblv1.Text = (nodelist2.InnerText); break;

I recieve this in my label:

2014-05-26T11:15:00+0200

How do i get only the:

11:15

I looked around here but i didn't find any results.

2 Answers 2

1

One option is to use parsed time data from DateTime:

var date = DateTime.Parse( "2014-05-26T11:15:00+0200", System.Globalization.CultureInfo.InvariantCulture);
var res = date.Hour + ":" + date.Minute;

Another way is direct parsing with regular expression:

var res = Regex.Match("2014-05-26T11:15:00+0200", @"\d{1,2}:\d{1,2}").Value;

Yet another way is to play with string.Split and similar, but I wouldn't do that if you care about you mental health...

Sign up to request clarification or add additional context in comments.

1 Comment

'Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems' - Jamie Zawinski
0

You can parse your time into a DateTime object and then present it:

DateTime dateTime;
if (DateTime.TryParse("2014-05-26T11:15:00+0200", out dateTime))
{
    lblv1.Text = string.Format("{0}:{1}", dateTime.Hour, dateTime.Minute);
}

1 Comment

this will return "11:15:00", not "11:15"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.