[英]Merge date and time to datetime: Input string was not in a correct format
I'm new to MVC and I encountered a problem and already spent lots of time on it. 我是MVC的新手,遇到了一个问题,已经花了很多时间。 Hope you could help me. 希望你能帮助我。
I want to combine [datetime]dateFrom and [timespan]timeFrom to [datetime]dtFrom. 我想将[datetime] dateFrom和[timespan] timeFrom组合到[datetime] dtFrom。 The reason I have to separate them is because I want to create date calender and time picker to pick up date and time. 我必须将它们分开的原因是因为我想创建日期日历和时间选择器以提取日期和时间。
Now I'm working with MS SQL Server and I can save dateFrom and timeFrom with no problem. 现在,我正在使用MS SQL Server,可以毫无问题地保存dateFrom和timeFrom。
How can I merge dateFrom and timeFrom and save in dtFrom? 如何合并dateFrom和timeFrom并保存在dtFrom中?
Here is my database entity. 这是我的数据库实体。
[Table("DaylightSavingsDate")]
public class DaylightSavingsDate 公共课程DaylightSavingsDate
{
[Column("iTimeZoneId")]
[Key]
[ForeignKey("Timezone")]
public virtual short TimeZoneId { get; set; }
[Column("dtFrom")]
public virtual DateTime? dtFrom { get; set; }
[Column("dtTo")]
public virtual DateTime? dtTo { get; set; }
[Column("DateFrom")]
[DataType(DataType.Date)]
public virtual DateTime dateFrom { get; set; }
[Column("TimeFrom")]
[DataType(DataType.Time)]
public virtual TimeSpan timeFrom { get; set; }
[Column("DateTo")]
[DataType(DataType.Date)]
public virtual DateTime dateTo { get; set; }
[Column("TimeTo")]
[DataType(DataType.Time)]
public virtual TimeSpan timeTo { get; set; }
public virtual STTimeZone Timezone { get; set; }
}
And here is the edit action in my controller 这是我控制器中的编辑动作
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "TimeZoneId,dtFrom,dtTo,dateFrom,dateTo,timeFrom,timeTo")] DaylightSavingsDate daylightsavingsdate)
{
if (ModelState.IsValid)
{
var dateFrom = daylightsavingsdate.dateFrom;
var timeFrom = daylightsavingsdate.timeFrom;
var dtFrom = daylightsavingsdate.dtFrom;
dtFrom = DateTime.Parse(dateFrom.ToString("yyyy-MM-dd ") + (timeFrom.ToString("hh:mm:ss")));
globalDb.Save(daylightsavingsdate);
return RedirectToAction("Index");
}
ViewBag.TimeZoneId = new SelectList(globalDb.TimeZones, "Id", "Name", daylightsavingsdate.TimeZoneId);
return View(daylightsavingsdate);
}
So I tried to parse dateFrom and timeFrom to DateTime to save in dtFrom but the error shows: "Input string was not in a correct format." 因此,我尝试将dateFrom和timeFrom解析为DateTime以保存到dtFrom中,但是错误显示:“输入字符串的格式不正确。”
Can anyone tell me how to solve this? 谁能告诉我如何解决这个问题? Really appreciated. 非常感谢。
Take parsing and formatting out of the picture if you can. 如果可以,请对图片进行解析和格式化。 You already have a DateTime
object and a TimeSpan
object. 您已经有一个DateTime
对象和一个TimeSpan
对象。 If the date was loaded without a time, then it's time is zero (midnight). 如果日期加载没有时间,则时间为零(午夜)。 So just add the TimeSpan
and you're done. 因此,只需添加TimeSpan
。
DateTime dateFrom = new DateTime(2014,1,1);
TimeSpan timeFrom = new TimeSpan(1,0,0);
DateTime dtFrom = dateFrom + timeFrom;
Console.WriteLine(dateFrom);
Console.WriteLine(timeFrom);
Console.WriteLine(dtFrom);
Output: 输出:
1/1/2014 12:00:00 AM
01:00:00
1/1/2014 1:00:00 AM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.