【问题标题】:save time with the desired date用所需的日期节省时间
【发布时间】:2013-05-08 04:06:47
【问题描述】:

下面是我的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.Text 中的这个日期来保存这个时间。

我该怎么做。

【问题讨论】:

    标签: c# datetime datetimepicker


    【解决方案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);
    

    如果您知道某种特定的文化,您可能想改用它。

    请注意,日期格式因文化而异。例如,值1/4/2013 可以解释为 1 月 4 日或 4 月 1 日,具体取决于您所在的世界。您需要了解文化,或者需要明确告诉您的用户使用哪种格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 2016-11-04
      • 2021-01-07
      • 2011-10-15
      • 1970-01-01
      • 2022-08-12
      • 1970-01-01
      相关资源
      最近更新 更多