Jump to content
  • Advertisement
Sign in to follow this  
-Gray Fox-

[.net] DateTime, Subtract and strings

This topic is 3829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a little problem: I have a string which contains a formatted time (example: string time = "12.45";) I want to subtract to the current time (DateTime.Now) the time in the string... and I don't know how to do that. I think I should use the Subtract function but I don't know how to pass the time variable to DateTime.Now.Subtract()... Should I convert it to an Integer?

Share this post


Link to post
Share on other sites
Advertisement
Well DateTime.Subtract takes either a DateTime or a Timespan. You need the DateTime version which means you must convert your time string to a DateTime object.

Use the DateTime.ParseExact method. If you google DateTimeFormatInfo you can find the MSDN documentation that explains all of the various codes for your custom format string. For example, assuming 12.45 means 12:45pm on a 24 hour clock, you would call the method like this:

DateTime myDate = DateTime.ParseExact("12.45", "HH.mm", null);


Make sense? Good luck.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!