當使用日期時間的.ParseExact()方法,我總是得到輸出作爲我把字符串同樣在這裏是我的代碼:DateTime.ParseExact沒有做什麼,我想要它做的
[Authorize(Roles = "Backoffice, Manager")]
[HttpPost]
public ActionResult FilmShowCreate(FilmShowViewModel newFilmShow)
{
if (ModelState.IsValidField("FilmId") && ModelState.IsValidField("Time"))
{
DateTime unformattedDateTime = newFilmShow.Date.Date + newFilmShow.Time.TimeOfDay;
string dateString = unformattedDateTime.ToString("yyyy-MM-dd HH:mm:ss");
DateTime dbDate = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm:ss",
CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.AdjustToUniversal);
FilmShow filmShow = new FilmShow
{
Film = filmRepository.GetFilm(newFilmShow.FilmId),
Cinema = cinemaRepository.GetCinema(newFilmShow.CinemaId),
ThreeDimensional = newFilmShow.ThreeDimensional,
Date = dbDate,
SpecialEvent = newFilmShow.SpecialEvent
};
filmShowsRepository.AddShow(filmShow);
return View("SuccesfullFilmShowCreate");
字符串dateString格式不錯,但它是一個字符串,我需要將它作爲格式DateTime存儲在數據庫中,如「year-month-day hours:minutes:seconds」。但無論出於何種原因,ParseExact在我的情況下似乎不起作用。我得到的DateTime格式是「dd-MM-yyyy HH:mm」。
嗨,謝謝你的回覆!我理解你的故事,但它並不能真正解決我的實際問題。當我在遷移時運行種子方法時,我添加了如下所示的日期時間:Date = new DateTime(2017,03,08,14,00,00)導致日期2017-03-08 14:00:00其中是我想要的,但是當我使用我的存儲庫添加日期時間時,我得到以下格式2017-08-03 14:00:00這是日和月轉換。我真的不知道這是什麼造成的,但是真的很讓人沮喪。 –
@NickBurggraaff對不起,我認爲你需要更多的調試。放心'ParseExact'不是你的問題。 – BradleyDotNET
感謝您的幫助!通過分配一天到一個月和一個月到一天來修復它。不是一個很乾淨的修復,但是是有效的......那是重要的。 –