How can I get a DateTime based on a string
e.g:
if I have mytime = "14:00"
How can I get a DateTime
object with current date as the date, unless current time already 14:00:01, then the date should be the next day.
You can use DateTime.TryParse()
: which will convert the specified string representation of a date and time to its DateTime
equivalent and returns a value that indicates whether the conversion succeeded.
string inTime="14:00";
DateTime d;
if(DateTime.TryParse(inTime,out d))
{
Console.WriteLine("DateTime : " + d.ToString("dd-MM-yyyy HH:mm:SS"));
}
Working example here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With