Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I need to use different values with a TimeSpan but I cannot get it to work. What I would like is something like this:

C#
TimeSpan mySpan = new TimeSpan();

mySpan = (Convert.ToInt16(DateTime.Parse(DataDoc.SelectSingleNode("/Resource/TimeSteps/TS[24]").InnerText).ToString("HH")), Convert.ToInt16(DateTime.Parse(DataDoc.SelectSingleNode("/Resource/TimeSteps/TS[24]").InnerText).ToString("mm")), 0);


But I cannot get this to work so how would I create a variable called mySpan and change its variable separately with this code:?

C#
Convert.ToInt16(DateTime.Parse(DataDoc.SelectSingleNode("/Resource/TimeSteps/TS[24]").InnerText).ToString("HH")), Convert.ToInt16(DateTime.Parse(DataDoc.SelectSingleNode("/Resource/TimeSteps/TS[24]").InnerText).ToString("mm")), 0);


Thank you.
Posted

There is a much simpler way.

First get your int parts from DateTime:
C#
DateTime myDate;

if (DateTime.TryParse(DataDoc.SelectSingleNode("/Resource/TimeSteps/TS[24]").InnerText, out myDate) {
   int hours = myDate.Hour;
   int minutes = myDate.Minute;


Then construct your TimeSpan:
C#
   TimeSpan ts = TimeSpan.FromHours(hours) + TimeSpan.FromMinutes(minutes);
}


EDIT: Seeing OriginalGriff's answer, I realize we're on Friday evening and that I should be drinking beer with friends instead of writing code. Here's an updated, even simpler solution:

C#
if (DateTime.TryParse(DataDoc.SelectSingleNode("/Resource/TimeSteps/TS[24]").InnerText, out myDate) {
   TimeSpan ts = new TimeSpan(myDate.Hour, myDate.Minute, 0);
}
 
Share this answer
 
v2
Comments
Menon Santosh 6-Sep-13 14:37pm    
my + 5
If I rip your code apart for a moment, and put it back together differently so we can see what you have got so far:
C#
TimeSpan mySpan = new TimeSpan();

string s1 = DataDoc.SelectSingleNode("/Resource/TimeSteps/TS[24]").InnerText;
DateTime dt1 = DateTime.Parse(s1);
short i1  = Convert.ToInt16(dt1.ToString("HH"));
string s2 = DataDoc.SelectSingleNode("/Resource/TimeSteps/TS[24]").InnerText;
DateTime dt2 = DateTime.Parse(s2);
short i2  = Convert.ToInt16(dt2.ToString("mm"));

mySpan = (i1, i2, 0);
And you are surprised it doesn't work? What is the last bit supposed to be? Or do? The compiler doesn't know, and frankly I have no idea either.
If you mean to construct a new TimeSpan then it's simple:
C#
mySpan = new TimeSpan(i1, i2, 0);
Will do it...
 
Share this answer
 
Comments
Menon Santosh 6-Sep-13 14:37pm    
my + 5
Henry Hunt 6-Sep-13 14:46pm    
Thank you...I now have this code but there is a problem:

int minutes = Convert.ToInt16(DateTime.Parse("2013-09-05T14:00:00").ToString("mm"));
int hours = Convert.ToInt16(DateTime.Parse("2013-09-05T14:00:00").ToString("HH"));

TimeSpan mySpan = new TimeSpan(minutes, hours, 0);

int totalSecondsSinceMidnight = Convert.ToInt32(mySpan.TotalMinutes);
MessageBox.Show(totalSecondsSinceMidnight.ToString());

It returns 14 and there surely aren't 14 minutes since midnight are there? Could you work out why this is? Thank you.
OriginalGriff 6-Sep-13 14:49pm    
Ahem:
new TimeSpan(minutes, hours, 0);
If you look at MSDN you will see it is the other way round:
new TimeSpan(hours, minutes, 0);
Henry Hunt 6-Sep-13 15:09pm    
Thank you, that worked.
OriginalGriff 6-Sep-13 15:16pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900