i have try many converting techniques but always failed
Like: I have in
[btnIN button]
lbltimein.text = dr["column_name"].toString();
[btnout button]
DateTime dt = Convert.ToDateTime(lbltimein.text);
i have try many converting techniques but always failed
Like: I have in
[btnIN button]
lbltimein.text = dr["column_name"].toString();
[btnout button]
DateTime dt = Convert.ToDateTime(lbltimein.text);
You need to know the Format in which user inputs the Date
Value
For example if the format i dd-MM-yyyy
Try This:
DateTime dt = DateTime.ParseExact(lbltimein.Text,"dd-MM-yyyy",
System.Globalization.CultureInfo.InvariantCulture) ;
EDIT: from the comments if you have the time in format of HH:mm:ss
You can Split it based on semicolon and assign it to TimeSpan
constructor to get the total hours.
var time = lbltimein.Text.Split(':');
TimeSpan time=new TimeSpan(Convert.ToInt32(time[0]),
Convert.ToInt32(time[1]),Convert.ToInt32(time[2]));
double totalHours = time.TotalHours;
EDIT 2: if you have format like : 08:00:00 AM
Try This:
var inputtime = lbltimein.Text;
TimeSpan time = new TimeSpan(Convert.ToInt32(inputtime.Substring(0,2)),
Convert.ToInt32(inputtime.Substring(3, 2)),
Convert.ToInt32(inputtime.Substring(6, 2)));
double totalHours = time.TotalHours;
DateTime.Parse(dr["column_name"].ToString())
should do the trick if your string is in a valid DateTime format.
If your not sure of the format, you can also try this to determine if your string can be converted. I prefer this method since it gives you a chance to handle bad input without try/catch blocks.
DateTime OutputTime = new DateTime();
if(DateTime.TryParse(dr["column_name"].ToString(),out OutputTime))
{
///manipulate output datetime here if successful
}
else
{
///figure out why the conversion failed / handle error
}