Sign in to follow this  
-Gray Fox-

[.net] DateTime, Subtract and strings

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this