datetime在本地工作但不在服务器上工作

时间:2014-07-27 08:12:03

标签: c# asp.net datetime

我有一个插入日期的表单..

在本地工作,当我提交表单时,它非常有效并将日期保存到服务器。

但是当我尝试从服务器执行此操作时,我收到错误:

  

字符串未被识别为有效的DateTime.System.Collections.ListDictionaryInternal

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

public partial class test : System.Web.UI.Page
{       
    protected void dog_add_submit_Click(object sender, EventArgs e)
    {
        /*Create and Populate Dog*/

        Dog d = new Dog();
        try
        {
            d.DogName = dog_add_name.Text;
            d.ImageUrl = SaveImages();
            d.Colour = dog_add_colour.Text;
            d.PlaceFrom = dog_add_placeFrom.Text;
            d.Breed = dog_add_breed.Text;

            d.ArrivalDate = DateTime.Parse(txtDate.Text);

            //if fields were populated properly in c# proceed to insert into database
            try
            {
                int numEffected = d.doginsert();
                // Response.Write("num of effected rows are " + numEffected.ToString());

                Response.Redirect("MedicalHistoryAdd.aspx?dogid=" + d.SqldogID);
            }
            catch (Exception ex)
            {
                Response.Write("There was an error when trying to insert the dog into     the database" + ex.Message);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message + ex.Data);
        }

    }
}

2 个答案:

答案 0 :(得分:3)

.NET会将您的语言环境设置为其运行的计算机上的默认语言环境。这意味着它还将解析该语言环境中的DateTime和ToString()DateTime。所以我的猜测是你的本地语言/时区偏好与服务器不同?

您可以在web.config中设置它们以期望并输出特定内容:

<system.web>
   <globalization culture="da-DK" uiCulture="da-DK" />
</system.web>

我是丹麦人,也许你想要en-GB或en-US?

第二个选择是使用不变格式提供者在桶中射鱼。

DateTime.Parse(txtDate.Text, DateTimeFormatInfo.InvariantInfo);

希望有所帮助!

答案 1 :(得分:0)

试试这个

DateTime curr = Convert.ToDateTime(txtDate.Text);
d.ArrivalDate = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(curr, "India Standard Time");

d.ArrivalDate = Convert.ToDateTime(txtDate.Text);