繁体   English   中英

发布到Web服务器后将字符串转换为DateTime时出错

[英]Error while converting string to DateTime after publishing to web server

我正在使用下面的代码

    DateTime dtt=new DateTime();
    dtt = Convert.ToDateTime(FromDate);


  //  DateTime dtt = DateTime.Parse(FromDate); //this also gives the same error

    con = new MySqlConnection(conString);
    con.Open();
    for (int i = 1; i <= TotalDays; i++)
    {         
        string updateHotelBooking = "Update tbl_hotelbookingdetail set `BookedRoom`=`BookedRoom`+"+1+", `AvailableRoom`=`TotalRoom`-`BookedRoom` where `HotelID`="+HotelID+" AND `CurrentDate`='"+dtt.ToString("dd-MM-yyyy")+"'";
        MySqlCommand cmd7=new MySqlCommand(updateHotelBooking,con);
        cmd7.ExecuteNonQuery();                
        dtt = dtt.AddDays(1);
    }

这段代码在我的一个web服务中,我用于iPhone应用程序。

这里的FromDate是一个带有值的字符串,在这个格式15-11-2011 ,它来自应用程序的字符串格式。 我正在将它转换为DateTime,因为在总天数的循环中I need to add day to dtt.

它在本地主机上运行正常,dtt值为15-11-2011 00:00:00

但是当我发布它时,它会给出错误

String was not recognize as valid DateTime

这几乎可以肯定是因为您的服务器默认使用不同的文化 - 而您的代码只是使用当前的线程文化。

可以使用DateTime.Parse指定它 - 或使用DateTime.ParseExactDateTime.TryParseExact显式指定模式 - 但我们需要更多地了解字符串的来源以建议最佳方法。 是来自用户吗? 如果是这样,您应该使用用户的文化来解析它。 它是一种特定的格式(例如来自XML文档)吗? 如果是,请使用该特定格式进行解析。

理想情况下,完全摆脱字符串部分 - 例如,如果您从数据库中获取它,是否可以存储它并将其作为 DateTime而不是字符串获取? 值得尝试尽可能减少涉及的字符串转换次数。

编辑:要从固定格式的dd-MM-yyyy解析,我会使用:

DateTime value;
if (DateTime.TryParseExact(text, "dd-MM-yyyy",
                           CultureInfo.InvariantCulture,
                           DateTimeStyles.AssumeUniversal,
                           out value))
{
     // Value will now be midnight UTC on the given date
}
else
{
    // Parsing failed - invalid data
}

您在本地计算机和服务器上的文化设置是什么?

DateTime转换取决于当前的文化 - 不同国家/地区的日期编写方式截然不同。

使转换“可预测”的一种方法是使用不变文化:

DateTime dtt = Convert.ToDateTime(FromDate, CultureInfo.InvariantCulture);

服务器日期格式可能是mm / dd / yyyy,你试图通过dd / mm / yyyy

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] dateValues = { "30-12-2011", "12-30-2011", 
                              "30-12-11", "12-30-11" };
      string pattern = "MM-dd-yy";
      DateTime parsedDate;

      foreach (var dateValue in dateValues) {
         if (DateTime.TryParseExact(dateValue, pattern, null, 
                                   DateTimeStyles.None, out parsedDate))
            Console.WriteLine("Converted '{0}' to {1:d}.", 
                              dateValue, parsedDate);
         else
            Console.WriteLine("Unable to convert '{0}' to a date and time.", 
                              dateValue);
      }
   }
}
// The example displays the following output:
//    Unable to convert '30-12-2011' to a date and time.
//    Unable to convert '12-30-2011' to a date and time.
//    Unable to convert '30-12-11' to a date and time.
//    Converted '12-30-11' to 12/30/2011.

请查看此内容以获取更多详

记录(或以其他方式向您自己提供反馈)FromDate是什么。 也许它是空的?

可能服务器上的语言设置不同,因此它无法识别dd-MM-yyyy - 尝试使用DateTime.ParseExact(dateString, "dd-MM-yyyy", CultureInfo.InvariantCulture);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM