Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert time string to DateTime in c#

Tags:

c#

datetime

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.

like image 675
Harts Avatar asked Mar 21 '16 02:03

Harts


1 Answers

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

like image 148
sujith karivelil Avatar answered Oct 12 '22 11:10

sujith karivelil