1

日付が 12 を過ぎた後の日付の取得に問題があります。たとえば、カレンダー エクステンダーからクリックすると、2013 年 2 月 7 日から 2013 年 7 月 19 日まで、次のエラーが表示されます。DateTimeで表される文字列は、カレンダー System.Globalization.GregorianCalendar ではサポートされていません。

これは私のコードです:

var format = "MM/dd/yyyy";
    DateTime one = DateTime.ParseExact(startdate, format, CultureInfo.InvariantCulture);
    DateTime two = DateTime.ParseExact(enddate, format, CultureInfo.InvariantCulture);



    if (two >= one)
    {
        SqlConnection conn = new SqlConnection("Data Source=""catalog="";Integrated Security=True");
        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT Name,CLass, NRIC, StallNo, AmountSpent ,TimeDate=convert(nvarchar,timedate,103)  FROM StudentTransactions WHERE TimeDate BETWEEN '" + one + "' AND '" + two + "'", conn);
        SqlDataReader reader = cmd.ExecuteReader();
        GridView1.DataSource = reader;
        GridView1.DataSourceID = null;
        GridView1.Visible = true;
        GridView1.DataBind(); 
        conn.Close();
   }
4

2 に答える 2

5

19/7/2013は有効な月ではないため、MM/dd/yyyy形式を使用して解析できません。代わり に使用することもできます。19
dd/MM/yyyy

于 2013-07-11T03:26:26.487 に答える
0

ローカルとサーバーの日時形式は、ラベルまたはテックスボックスで日付形式を表示し、データベースに更新を追加する場合に何度も異なります。長い疲労の後、私はこの解決策を得ました: まず、以下のような現在の日付形式を確認してください:

lblMsg.Text = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;  
if (lblMsg.Text == "dd/MM/yyyy")  
  txtd.Text = DateTime.Parse(dr["EventDate"].ToString()).ToString("dd/MM/yyyy");  
else  
  txtd.Text = DateTime.Parse(dr["EventDate"].ToString()).ToString("MM/dd/yyyy");

追加または更新: 同様に、現在の日付形式が dd/MM/yyyy または "MM/dd/yyyy" であることを確認できます。

if (lblMsg.Text == "dd/MM/yyyy")
 usinfo.BDate = DateTime.ParseExact(txtDOB.Text.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
else
usinfo.BDate = DateTime.ParseExact(txtDOB.Text.ToString(), "MM/dd/yyyy", CultureInfo.InvariantCulture);

lblMsg.Text = "";
于 2015-10-22T11:57:32.340 に答える