0

I have string time. lets say '06:35 PM'. I want to convert the string to DateTime. The date must be current time(the day as they input the time).

I did

string times = endTime;
DateTime dt;
(DateTime.TryParseExact(times, "YYYY-MM-dd HH:mm tt", CultureInfo.InvariantCulture,DateTimeStyles.None, out dt))

But it didn't works. it make a null value. because when I try to put the result on label, the label did not show anything. and also I have been try

var date = DateTime.Parse("06:45 AM");
Console.WriteLine(date);

But it didn't works too and it makes an error.

ERROR System.Data.SqlClient.SqlException (0x80131904): The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated. "

How do I convert it ?

Sangram Chavan
  • 331
  • 1
  • 9
azalikaEriya
  • 195
  • 1
  • 4
  • 17
  • 4
    The code you have included has nothing to do with sql, are you sure this is the part of code that is producing the error? – Sayse Jan 15 '15 at 08:35
  • 2
    Please post the correct code. This is SQL Server complaining yet you have posted C# code that has nothing to do with SQL (Server). – Lasse V. Karlsen Jan 15 '15 at 08:36
  • Is `endTime` a `"06:45 AM"`? – Soner Gönül Jan 15 '15 at 08:37
  • The DateTime code pasted has nothing to do the exception. It is talking about a SqlException. Paste in the code where database is being called. For a parameterised query or stored procedure, check the values that are being passed through. Somewhere you are sending a parameter to a SQL datetime column that contains an invalid value. – Murray Foxcroft Jan 15 '15 at 08:46
  • yes, endTime = "06:45 AM" so, do you mean this error cause by sql statement ? I have some value to insert in database. if I do not insert the endTime value, it'll fine. the value is inserted. but if I do insert the endTime on sql statemen. it do not insert the data. the row on sql is not added. @MurrayFoxcroft – azalikaEriya Jan 15 '15 at 08:58
  • possible duplicate of [C# Convert String to DateTime (AM : PM)](http://stackoverflow.com/questions/27958164/c-sharp-convert-string-to-datetime-am-pm) – Tijo Tom Jan 15 '15 at 09:18
  • Added an answer below with more detail to get you pointed in the right direction. Good luck! – Murray Foxcroft Jan 15 '15 at 09:21

3 Answers3

1

If day doesn't matter, you can use following

DateTime date;
if (DateTime.TryParseExact("06:45 AM", new[] {"h:mm tt"}, null, DateTimeStyles.None, out date))
{
    Console.WriteLine(date);
    Console.WriteLine(date.TimeOfDay);
}
Sangram Chavan
  • 331
  • 1
  • 9
1

I think you are passing the string "06:45 AM" to the database and this is not a valid entry since the DB does not know how to store it, hence the exception.

Looking a bit further, in your first example, YYYY should be lowercase yyyy. YYYY will not parse properly correctly in to a date format.

Considering the second example you have two options:

1) When writing to the database, make sure you pass a valid date, e.g. below. You may need to try a few formats to match up to what your DB expects but it will need a full date and time.

var date = string.Format("{0:yyyy-MM-dd HH:mm tt}", DateTime.Parse("06:45 AM"));
Console.WriteLine(date);

2) Use a full DateTime approach using the correct SQL parameter type. This looks like a good explanation covering a few gotchas. Using DateTime in a SqlParameter for Stored Procedure, format error

Community
  • 1
  • 1
Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86
0

I think the below code will solve your issue;

DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(06, 45, 0);
dt = dt.Date + ts;

To convert string to DateTime format, use the below code;

string date = "01/08/2008";
DateTime dt = Convert.ToDateTime(date); 
Tijo Tom
  • 487
  • 6
  • 13