使用所需日期节省时间

本文关键字:日期 节省时间 | 更新日期: 2023-09-27 18:12:39

下面是我的html代码

 <MKB:TimeSelector ID="TimeFrom" runat="server" DisplaySeconds="False">
                                                </MKB:TimeSelector>

以及具有日期的文本框。

VehicleBookingDate.Text

我想在数据库中保存日期和时间。如果我想在下面这样做

 string t1 = tsTimeFrom.Hour.ToString() + ":" + tsTimeFrom.Minute.ToString() + " " + tsTimeFrom.AmPm.ToString();
 DateTime Time_From = Convert.ToDateTime(t1);

它用当前日期保存时间,因为我想用VehicleBookingDate中的这个日期保存这个时间。文本

我怎么能那样做。

使用所需日期节省时间

// 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日,这取决于你所在的世界。你要么需要有文化意识,要么需要明确地告诉用户要使用什么格式。