C# 用所需日期节省时间

C# 用所需日期节省时间,c#,datetime,datetimepicker,C#,Datetime,Datetimepicker,下面是我的html代码 <MKB:TimeSelector ID="TimeFrom" runat="server" DisplaySeconds="False"> </MKB:TimeSelector> 我想在数据库中保存日期和时间。如果我想像下面这样做的话 string t1 = tsTimeFrom.Hour.ToString() + ":" + tsTimeFro

下面是我的html代码

 <MKB:TimeSelector ID="TimeFrom" runat="server" DisplaySeconds="False">
                                                </MKB:TimeSelector>
我想在数据库中保存日期和时间。如果我想像下面这样做的话

 string t1 = tsTimeFrom.Hour.ToString() + ":" + tsTimeFrom.Minute.ToString() + " " + tsTimeFrom.AmPm.ToString();

 DateTime Time_From = Convert.ToDateTime(t1);
它用当前日期保存时间,我想用VehicleBookingDate.Text中的日期保存时间

我该怎么做呢

// use a string formatter to pull it all together
string s = string.Format("{0} {1}:{2} {3}",
                         VehicleBookingDate.Text,
                         tsTimeFrom.Hour,
                         tsTimeFrom.Minute,
                         tsTimeFrom.AmPm);

// You can parse it this way, which will assume the current culture settings
DateTime Time_From = DateTime.Parse(s);

// Or you can be much more specific - which you probably should do.
DateTime Time_From = DateTime.ParseExact(s,
                                         "d/M/yyyy hh:mm:ss tt",
                                         CultureInfo.InvariantCulture);
如果你知道的话,你可能想使用一种特定的文化

请注意,不同文化的日期格式差异很大。例如,值
1/4/2013
可以解释为1月4日或4月1日,具体取决于您所在的地区。您要么需要了解区域性,要么需要明确告诉用户要使用什么格式

// use a string formatter to pull it all together
string s = string.Format("{0} {1}:{2} {3}",
                         VehicleBookingDate.Text,
                         tsTimeFrom.Hour,
                         tsTimeFrom.Minute,
                         tsTimeFrom.AmPm);

// You can parse it this way, which will assume the current culture settings
DateTime Time_From = DateTime.Parse(s);

// Or you can be much more specific - which you probably should do.
DateTime Time_From = DateTime.ParseExact(s,
                                         "d/M/yyyy hh:mm:ss tt",
                                         CultureInfo.InvariantCulture);