I have a DateTime
object, I'm getting that DateTime
object from some WPF
component. Now, that object will be in 12h format
. My goal is to transform that object to 24h DateTime
object. So, no strings in any directions whatsoever.
Example:
If the user sets on WPF
component something like: 01.24.2020 06:00 PM
, I need to convert that object to something like:
new DateTime(1, 24, 2020, 18, 00);
Here is my failed attempt:
private DateTime convertDatesToDBFormat(DateTime start)
{
var format = start.ToString("tt", CultureInfo.InvariantCulture);
int hours = format.Equals("Pm") ? start.Hour + 12 : start.Hour;
return new DateTime(start.Year, start.Month, hours, start.Minute, 0, 0);
}
Also, I'm not really sure what tt
means here...
EDIT: Later, when I want to save to database, this is how I'm doing it, when I was using other WPF components (which were giving me desired "form" of DateTime):
new SqlParameter("@StartDate", Utility.convertDateWithTimeToString(appointment.Period.Start)); //Start property returns DateTime
public static string convertDateWithTimeToString(DateTime date)
{
return date.ToString("dd-MM-yyyy hh:mm");
}
And with this, column Start in SQL Server looked like this:
24-01-2020 18:00
But with this new WPF component, I end up in db with this:
24-01-2020 06:00 //even tho I wanted in the evening time 18:00 not 06:00am