adding 24 hours onto DateTime variable
-
hi I cant seem to add 24 hours onto a datetime variable. i have a startTime value and with that i want to add 24 hours to it to get a endTime. what i have right now is:
DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]); DateTime endTime = startTime; endTime.AddHours(24);
startTime's value is for eg. "2007/06/01 12:00:00AM" but its not adding 24hours to endTime variable. It stays the same as the startTime. Please somebody help me.. -
hi I cant seem to add 24 hours onto a datetime variable. i have a startTime value and with that i want to add 24 hours to it to get a endTime. what i have right now is:
DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]); DateTime endTime = startTime; endTime.AddHours(24);
startTime's value is for eg. "2007/06/01 12:00:00AM" but its not adding 24hours to endTime variable. It stays the same as the startTime. Please somebody help me..try this instead:
DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]);
DateTime endTime = startTime.AddHours(24);
Without checking the docs I strongly suspect
AddHours
returns a new DateTime instead of modifying the existing one.-- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.
-
hi I cant seem to add 24 hours onto a datetime variable. i have a startTime value and with that i want to add 24 hours to it to get a endTime. what i have right now is:
DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]); DateTime endTime = startTime; endTime.AddHours(24);
startTime's value is for eg. "2007/06/01 12:00:00AM" but its not adding 24hours to endTime variable. It stays the same as the startTime. Please somebody help me..Hi, the DateTime methods return a new DateTime, they do not change an existing one, hence you need
endTime=endTime.AddHours(24);
:)Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
try this instead:
DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]);
DateTime endTime = startTime.AddHours(24);
Without checking the docs I strongly suspect
AddHours
returns a new DateTime instead of modifying the existing one.-- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.
dan neely wrote:
Without checking the docs I strongly suspect AddHours returns a new DateTime instead of modifying the existing one
Correct -
DateTime
is immutable.
Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
-
hi I cant seem to add 24 hours onto a datetime variable. i have a startTime value and with that i want to add 24 hours to it to get a endTime. what i have right now is:
DateTime startTime = DateTime.Parse(Request.QueryString["LogonDate"]); DateTime endTime = startTime; endTime.AddHours(24);
startTime's value is for eg. "2007/06/01 12:00:00AM" but its not adding 24hours to endTime variable. It stays the same as the startTime. Please somebody help me..As others have said, the methods on DateTime do not modify the object you have. That is because the DateTime object is immutable. That means that once it is created it cannot be changed, you have to create a new one. Had you done the simplest of research - like read the documentation - you would have found this out. (Hint: In pretty much all windows applications pressing F1 will bring up the documentation.)
Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
-
As others have said, the methods on DateTime do not modify the object you have. That is because the DateTime object is immutable. That means that once it is created it cannot be changed, you have to create a new one. Had you done the simplest of research - like read the documentation - you would have found this out. (Hint: In pretty much all windows applications pressing F1 will bring up the documentation.)
Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
Perhaps his F1 key is immutable and mapped to 'Switch Weapons' or 'Strafe'. (Do games still have strafe, like the original Doom?)
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
"This time yesterday, I still had 24 hours to meet the deadline I've just missed today."
-
Perhaps his F1 key is immutable and mapped to 'Switch Weapons' or 'Strafe'. (Do games still have strafe, like the original Doom?)
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
"This time yesterday, I still had 24 hours to meet the deadline I've just missed today."
Malcolm Smart wrote:
Perhaps his F1 key is immutable and mapped to 'Switch Weapons' or 'Strafe'. (Do games still have strafe, like the original Doom?)
I doubt he's up to any game beyond Snap.
Deja View - the feeling that you've seen this post before.