Kapryśna metoda parsująca DateTime

0

Napotkałem dzisiaj w aplikacji ciekawy problem. Mianowicie, wszystko na localhoście działa aż miło, problem pojawił się po publikacji na hostingu. Otóż, mam metode, która na podstawie danych wejściowych z bazy danych parsuje to na klase DataTime. Zarówno na localu jak i na hostingu korzystam z tej samej bazy. Jak można zauważyć, dane są identyczne, jednak tutaj wyjątek się nie pojawia, a tam się pojawia. Proszę o pomoc.

To localhost jak widać po debugu

title

A to część logów powyższego kodu z hostingu


DEBUG 2020-04-17 00:08:09,056 363393ms ManageController       ntNewNotifications - 04/16/20 14:14:40
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:40
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:41
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:41
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:41
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:41
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:42
DEBUG 2020-04-17 00:08:09,087 363425ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:42
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:42
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:43
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:43
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:43
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:43
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:44
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:44
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:44
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:45
DEBUG 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,103 363440ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:45
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:45
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:46
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:46
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:47
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:47
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:47
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:47
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:48
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:48
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:48
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:49
DEBUG 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,118 363456ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.
   at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
   at ASP._Page_Areas_Admin_Views_Manage_Index_cshtml._CountNewNotifications()
DEBUG 2020-04-17 00:08:09,134 363472ms ManageController       ntNewNotifications - 04/16/20 14:14:49
DEBUG 2020-04-17 00:08:09,134 363472ms ManageController       ntNewNotifications - 04/16/20 14:14:39
ERROR 2020-04-17 00:08:09,134 363472ms ManageController       ntNewNotifications - System.FormatException: String was not recognized as a valid DateTime.

4

Skoro masz daty po hamburgerowemu, to albo ustaw format regionalny na serwerze na hamburgerowy, albo parsuj z wymuszeniem takiego, czyli np.:

var ziemniak = DateTime.Parse(x.DateCreated, new CultureInfo("en-us"));
5

Dlaczego w modelu trzymasz stringa z datą? Trzymaj tam datę z datą...

1

ogólnie to co napisał @ŁF a jak już się upierasz trzymajć stringi to ParseExact

0

Faktycznie, przeoczyłem format. Wszystko teraz działa jak nalezy. Dzięki za pomoc!

1 użytkowników online, w tym zalogowanych: 0, gości: 1