.net c#转换英国日期格式为美国

Convert UK date format to American
2021-09-14
  •  译文(汉语)
  •  原文(英语)

我有一个jQuery日期选择器,该日期选择器将日期放入格式为DD / MM / YYYY的文本框中.

我正在尝试将其存储在需要以MM / DD / YYYY格式接受日期的SQL Server 2008中,如何格式化日期才能使其正常工作?

这是向查询中添加my参数的代码,由于日期和月份的计算方式错误,因此会导致错误

 TextBox fixtureDateTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMenu").FindControl("datepicker");
 SqlParameter fixtureDateParam = new SqlParameter("@fixtureDate", fixtureDateTextBox.Text);
速聊1:
检查一下,应该是您要找的东西.stackoverflow.com/a/15547264/2171266
速聊2:
SqlParameters.AddWithValue()使用与SqlParameter相反的方法使用Matt查找,也将值作为变量传递给您的参数,而不是明文或硬编码的文本Avoind SQL Injection
解决过程1

绝对不要在执行操作时直接将输入文本插入参数-这是太多安全漏洞和漏洞的根本原因.DateTimeOffset首先DateTime,通过调用将输入解析为对象(也可以使用,但通常最好使用Offset)DateTimeOffset.Parse(...),然后可以将其作为参数添加而无需修改.

编辑:我重新阅读了您的问题,并意识到虽然以上内容没有错,但它确实错过了一些解决您问题的重要事项.请参阅以下内容,了解如何配置日期选择器以显示其存储在备用字段中的备用日期格式:

编辑2:您将要DateTime使用允许您传入IFormatProvider实例的重载来解析对象.有关如何执行您要执行的操作的示例,请参见http://msdn.microsoft.com/zh-cn/library/system.iformatprovider.aspx.否则,DateTime.Parse('13/3/2013')将由于无法识别的格式而失败.

DateTime.Parse('13/3/2013', new CultureInfo("en-GB").DateTimeFormat) 是您要使用输入框执行的操作.

解决过程2

我与其他人同意,你应该能够配置JQuery的选择器格式开始的,但如果你不能,这应该做的伎俩:

TextBox fixtureDateTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMenu").FindControl("datepicker");
DateTime fixtureDate = DateTime.Parse(fixtureDateTextBox.Text, new CultureInfo("en-GB"));
SqlParameter fixtureDateParam = new SqlParameter("@fixtureDate", fixtureDate.ToString(new CultureInfo("en-US"));
速聊1:
您的解决方案是一个可靠的解决方案,因为在该日期调用ToString时,它不假设有关CurrentUICulture的任何信息
解决过程3

试试这个:

TextBox fixtureDateTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMenu").FindControl("datepicker");
DateTime date = DateTime.Parse(fixtureDateTextBox.Text);
SqlParameter fixtureDateParam = new SqlParameter("@fixtureDate", date);
速聊1:
如果您的CultureInfo除了以下内容en-UK(或遵循该日历日期格式约定的区域性信息),则将失败
解决过程4

在名称空间部分添加: using System.Globalization;

然后解析您的输入日期,如下所示:

string DB_Date = (DateTime.Parse(fixtureDateTextBox.Text)).ToString(new CultureInfo("en-US"));
速聊1:
如果CurrentCulture不是支持该日期格式的样式,那仍然会失败
解决过程5
  DateTime UKDate = Convert.ToDateTime(datepicker.Text);<br>
  string usDate= UKDate.ToString("dd-MM-yyyy");<br>
  SqlParameter fixtureDateParam = new SqlParameter("@fixtureDate", usDate);

I have a JQuery date picker which puts the date into a textbox in format DD/MM/YYYY.

I am trying to store this in SQL Server 2008 which needs to accept the date as MM/DD/YYYY, how should I format the date to get this to work correctly?

This is the code to add a my parameter to the query, which causes an error as the day and the month are the wrong way round

 TextBox fixtureDateTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMenu").FindControl("datepicker");
 SqlParameter fixtureDateParam = new SqlParameter("@fixtureDate", fixtureDateTextBox.Text);
Talk1:
Check this out, should be what you are looking for. stackoverflow.com/a/15547264/2171266
Talk2:
Matt look up using SqlParameters.AddWithValue() method use that opposed to SqlParameter also pass in values to your parameters as variables not as clear or hard coded text Avoind SQL Injection
Solutions1

Never, ever directly insert input text into a parameter as you're doing - it's the root cause of too many security vulnerabilities and holes. Parse the input into a DateTimeOffset object first (you can also use DateTime, but the Offset is better in general to use) by calling DateTimeOffset.Parse(...), then you can simply add it as a parameter without modifying it.

EDIT: I re-read your question, and realized that while the above isn't wrong, it does miss some important things that address your question. See this for how to configure the date picker to display an alternate date format from what it stores in an alternate field:

EDIT 2: You'll want to parse your DateTime object using the overload which allows you to pass in an IFormatProvider instance. See http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx for an example of how to do what you're looking to do. Otherwise, DateTime.Parse('13/3/2013') will fail due to unrecognized format.

DateTime.Parse('13/3/2013', new CultureInfo("en-GB").DateTimeFormat) is what you want to do with the input box.

Solutions2

I agree with the other folks that you should be able to configure the JQuery picker format to begin with, but if you can't, this should do the trick:

TextBox fixtureDateTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMenu").FindControl("datepicker");
DateTime fixtureDate = DateTime.Parse(fixtureDateTextBox.Text, new CultureInfo("en-GB"));
SqlParameter fixtureDateParam = new SqlParameter("@fixtureDate", fixtureDate.ToString(new CultureInfo("en-US"));
Talk1:
yours is a robust solution because it doesn't assume anything about the CurrentUICulture when calling ToString on that date
Solutions3

Try this:

TextBox fixtureDateTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMenu").FindControl("datepicker");
DateTime date = DateTime.Parse(fixtureDateTextBox.Text);
SqlParameter fixtureDateParam = new SqlParameter("@fixtureDate", date);
Talk1:
that will fail if your CultureInfo is anything but en-UK (or a culture info that follows that calendar date format convention)
Solutions4

Add in the namespaces section: using System.Globalization;

then parse your input date as follows:

string DB_Date = (DateTime.Parse(fixtureDateTextBox.Text)).ToString(new CultureInfo("en-US"));
Talk1:
That will still fail if the CurrentCulture isn't one that supports that date format style
Solutions5
  DateTime UKDate = Convert.ToDateTime(datepicker.Text);<br>
  string usDate= UKDate.ToString("dd-MM-yyyy");<br>
  SqlParameter fixtureDateParam = new SqlParameter("@fixtureDate", usDate);
转载于:https://stackoverflow.com/questions/15576510/convert-uk-date-format-to-american

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入