Djeroen
Djeroen

Reputation: 571

change xml data in c#

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.

Upvotes: 1

Views: 66

Answers (2)

st4hoo
st4hoo

Reputation: 2204

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...

Upvotes: 1

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149608

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

Upvotes: 0

Related Questions