-4

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);
Rojan
  • 51
  • 2
  • 7
  • 2
    What is the string value of `lbltimein.Text`, You may need [Custom DateTime Format](http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx) with `DateTime.ParseExact` or `DateTime.TryParseExact` – Habib Mar 27 '14 at 17:18
  • 1
    What does the string that you're trying to convert look like? – Smeegs Mar 27 '14 at 17:18
  • See if it helps: http://stackoverflow.com/questions/5590180 – Andre Figueiredo Mar 27 '14 at 17:19
  • in lbltimein.text it has the timein Time in database. i need to convert that lbltime.text to datetime to come up with time out and total hours of work – Rojan Mar 27 '14 at 17:20
  • Please be more specific about your input, expected output, and actual output. – tnw Mar 27 '14 at 17:20
  • 1
    If there is an error converting, then the date string may not be in a convertable date format. We need to actually see the string thats stored in `lbltimein.text` – Smeegs Mar 27 '14 at 17:21
  • i have my timein Time in lbltimein.text and i want it to convert to date so that i can compute the totalhours of work – Rojan Mar 27 '14 at 17:23
  • the string of 12:01:35: AM in lbltimeine.text this string is inside my datebase – Rojan Mar 27 '14 at 17:52
  • string s = lblDueDate.Text; DateTime DueDate = DateTime.ParseExact(s, "MM-dd-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None); Itry this but its getting me error. i think my error is from the format of date but only i need is the time to format? how can i do that? And I tris the result is the same error: string s = lblDueDate.Text; DateTime DueDate = DateTime.ParseExact(s, "hh:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None); – Rojan Mar 28 '14 at 00:47

2 Answers2

1

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;
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • the format of my lbltimein in my database is like 08:00:00 AM – Rojan Mar 27 '14 at 17:29
  • its ok now. how can i compute for the timeout and subtract it to the lbltimein.text to get the total hours correctly. because when i click the btntimeout button ill get the lbltimein.text and subtract it to the datetime now so that i can get the total hrs. – Rojan Mar 27 '14 at 17:59
  • the edit2 is working is the Timespan time is for lbltimein.text? how can i subtract it to the current time? – Rojan Mar 28 '14 at 00:39
  • string s = lblDueDate.Text; DateTime DueDate = DateTime.ParseExact(s, "MM-dd-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None); Itry this but its getting me error. i think my error is from the format of date but only i need is the time to format? how can i do that? And I tris the result is the same error: string s = lblDueDate.Text; DateTime DueDate = DateTime.ParseExact(s, "hh:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None); – Rojan Mar 28 '14 at 00:47
  • DateTime dt = DateTime.ParseExact(lbltimein.Text,"dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture) ; DateTime dt2 = Convert.ToDateTime(DateTime.Now.ToShortTimeString()); Timespan ts = dt2 - dt; but im getting the error of the 1st line says that: FormatException was unhandled :String was not recognized as a valid – Rojan Mar 28 '14 at 00:50
  • what is the meening of substring(0,2)(3,2)(6,2) – Rojan Mar 28 '14 at 01:59
  • substring(0,2) gives the hours,substring(3,2) gives the minutes,substring(6,2) gives the seconds – Sudhakar Tillapudi Mar 28 '14 at 04:31
0
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
}
Lee Harrison
  • 2,306
  • 21
  • 32
  • I got this error where i try it. FormatException was unhandled :String was not recognized as a valid DateTime. – Rojan Mar 27 '14 at 17:25
  • So your input is not in a standard DateTime format. You'll need to change the text of the string to match a valid DateTime format. What is the exact string you are trying to parse? – Lee Harrison Mar 27 '14 at 17:26
  • its only a time like this "hh:mm:ss tt" – Rojan Mar 27 '14 at 17:40