c#datetime.tryParseExact选择CultureInfo

编程入门 行业动态 更新时间:2024-10-18 16:54:22
本文介绍了c#datetime.tryParseExact选择CultureInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嘿伙计们,我正在使用这段代码:

Hey guys, I'm using this code:

string date = eventStartDate.Text; string hour = eventStartHour.Text; string min = eventStartMinute.Text; //Set datestring and the format string dateString = date + " " + hour + ":" + min; string[] format = new string[] { "d/M/yyyy h:mm", "d/M/yyyy h:mm:ss", "d-M-yyyy h:mm", "dd-MM-yyyy hh:mm" }; DateTime dateTime; DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime); Console.WriteLine(dateTime);

它正在工作,但只是没有给出我希望它的格式...我想要一些类似的东西: dd / MM / yyyy hh:mm或dd-MM-yyyy hh:mm我应该使用哪种CultureInfo对于那个? 我这样做是为了将它们传递给DB:

and it's working but just not giving the format I want it to... I want something along the lines of: "dd/MM/yyyy hh:mm" or "dd-MM-yyyy hh:mm" Which CultureInfo should I use for that? I'm doing this to pass them to the DB:

protected void submit_Click(object sender, EventArgs e) { DateTime eventStart = dates(eventStartDate.Text, eventStartHour.Text, eventStartMinute.Text); DateTime departure = dates(departureDate.Text, departureHour.Text, departureMinute.Text); DateTime arrival = dates(arrivalDate.Text, arrivalHour.Text, arrivalMinute.Text); Console.WriteLine(arrival); DateTime flightDeparture = dates(flightDepartureStartDate.Text, flightDepartureStartHour.Text, flightDepartureStartMinute.Text); DateTime flightReturn = dates(flightReturnStartDate.Text, flightReturnStartHour.Text, flightReturnStartMinute.Text); byte[] file = (byte[])Session["File"]; string filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName); string ConStr = ConfigurationManager.ConnectionStrings["FORgestIT"].ToString(); SqlConnection SQLConn = new SqlConnection(ConStr); //The SQL Connection SqlCommand SQLCmd = new SqlCommand(); SQLCmd.Connection = SQLConn; SQLCmd.CommandType = CommandType.Text; SQLCmd.CommandText = "INSERT INTO Event (Name, Project, Objectives, City, Country, Event_Start, Departure, Arrival, Registration, National_Transportation, Accomodation, AC_NumberNights, AC_PreferHotelURL, Flight, FL_departure, FL_Depart_Prefer, FL_Depart_URL, FL_Return, FL_Ret_Prefer, FL_RET_URL, Notes, File, Status) VALUES (@Name,@Project,@Objectives,@City,@Country,@Event_Start,@Departure,@Arrival,@Registration,@National_Transportation,@Accomodation,@AC_NumberNights,@AC_PreferHotel,@AC_PreferHotelURL,@Flight,@FL_Departure,@FL_Depart_Prefer,@FL_Depart_URL,@FL_Return,@FL_Ret_Prefer,@FL_Ret_URL,@Notes,@File,@Status)"; SQLCmd.Parameters.Clear(); SQLCmd.Parameters.AddWithValue("@Name", name.Text); SQLCmd.Parameters.AddWithValue("@Project", project.Text); SQLCmd.Parameters.AddWithValue("@Objectives", objectives.Text); SQLCmd.Parameters.AddWithValue("@City", venueCity.Text); SQLCmd.Parameters.AddWithValue("@Country", venueCountry.Text); SQLCmd.Parameters.AddWithValue("@Event_Start", eventStart); SQLCmd.Parameters.AddWithValue("@Departure", departure); SQLCmd.Parameters.AddWithValue("@Arrival", arrival); SQLCmd.Parameters.AddWithValue("@Registration", registrationInformation.Text); SQLCmd.Parameters.AddWithValue("@National_Transportation", rdlYesNo.SelectedValue); SQLCmd.Parameters.AddWithValue("@Accomodation", accomodation.SelectedValue); SQLCmd.Parameters.AddWithValue("@AC_NumberNights", numberOfNights.Text); SQLCmd.Parameters.AddWithValue("@AC_PreferHotel", preferredHotel.Text); SQLCmd.Parameters.AddWithValue("@AC_PreferHotelURL", preferredHotelURL.Text); SQLCmd.Parameters.AddWithValue("@Flight", flight.SelectedValue); SQLCmd.Parameters.AddWithValue("@FL_Departure", flightDeparture); SQLCmd.Parameters.AddWithValue("@FL_Depart_Prefer", flightDeparturePreferred.Text); SQLCmd.Parameters.AddWithValue("@FL_Depart_URL", flightDeparturePreferredURL.Text); SQLCmd.Parameters.AddWithValue("@FL_Return", flightReturn); SQLCmd.Parameters.AddWithValue("@FL_Ret_Prefer", flightReturnPreferred.Text); SQLCmd.Parameters.AddWithValue("@FL_Ret_URL", flightReturnPreferredURL.Text); SQLCmd.Parameters.AddWithValue("@Notes", notes.Text); SQLCmd.Parameters.AddWithValue("@File", file); SQLCmd.Parameters.AddWithValue("@Status", "Pending"); if (SQLConn.State == ConnectionState.Closed) { SQLConn.Open(); } SQLCmd.ExecuteNonQuery(); SQLConn.Close(); string url = "Default.aspx"; ClientScript.RegisterStartupScript(this.GetType(), "callfunction", "alert('Your request form was saved correctly!');window.location.href = '" + url + "';", true); }

推荐答案

DateTime是二进制格式,这意味着它将值存储为数字而没有任何格式。 。 当你将dateTime传递给WriteLine时,你实际上在它上面运行了DateTime的ToString方法,但没有任何格式化(测量它将使用当前文化)... 如果你想获得特定的格式,你应该将格式字符串作为参数传递,如下所示: DateTime is a binary format, which means that it stores the value as a number without any formatting... When you pass dateTime to WriteLine you actually run ToString method of DateTime on it, but without any formatting (that measn it will use the current culture)... If you want to get a specific formatting you should pass the format string as parameter, like this: string format = "dd/MM/yyyy hh:mm"; Console.WriteLine(dateTime.ToString(format));

msdn.microsoft/en-us/library/zdtaw1bw(v = vs.110).aspx [ ^ ]

更多推荐

c#datetime.tryParseExact选择CultureInfo

本文发布于:2023-11-07 19:43:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:datetime   tryParseExact   CultureInfo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!