c#datetime.tryParseExact选择CultureInfo [英] c# datetime.tryParseExact choosing CultureInfo

查看:57
本文介绍了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));



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


这篇关于c#datetime.tryParseExact选择CultureInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆