出错发布到Web服务器后,将字符串转换为DateTime [英] Error while converting string to DateTime after publishing to web server

查看:208
本文介绍了出错发布到Web服务器后,将字符串转换为DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码

    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应用程序。

This code is in one of my webservice which I am using for iPhone application.

在这里寄件者是串在这甲值 2011-11-15 这是从字符串格式应用的到来。我因为在总天数
的循环将其转换为DateTime 我需要一天加入DTT。

here FromDate is string with value in this formate 15-11-2011 which is coming from the application in string format. I am converting it to DateTime because in loop of total days I need to add day to dtt.

这是本地主机上工作的罚款DTT值 2011-11-15 00:00:00

It is working fine on local host with dtt value 15-11-2011 00:00:00

但是当我发表它,它给误差

but when I published it,it gives error

字符串未被识别为有效的DateTime

推荐答案

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

This is almost certainly because your server uses a different culture by default - and your code is just using the current thread culture.

您的可以的指定此使用的 DateTime.Parse - 或显式的 DateTime.ParseExact 或的 DateTime.TryParseExact - 但我们需要知道的更多关于其中字符串是来自建议最好的办法。它是从用户?如果是这样,你应该使用用户的文化解析它。它是一个特定的格式(例如,从XML文档)代替?如果是这样,解析使用特定格式

You can specify this using DateTime.Parse - or specify the pattern explicitly with DateTime.ParseExact or DateTime.TryParseExact - but we need to know more about where the string is coming from to suggest the best approach. Is it from the user? If so, you should use the user's culture to parse it. Is it a specific format (e.g. from an XML document) instead? If so, parse using that specific format.

在理想情况下,摆脱了线的部分完全 - 如果你从例如数据库中获取它,你可以存储它把它拿来的的一个的DateTime ,而不是作为一个字符串?这是值得尝试,以减少尽可能参与字符串转换次数

Ideally, get rid of the string part entirely - if you're fetching it from a database for example, can you store it and fetch it as a DateTime instead of as a string? It's worth trying to reduce the number of string conversions involved as far as possible.

编辑:从DD-MM-YYYY我会用一个固定的格式解析:

To parse from a fixed format of dd-MM-yyyy I would use:

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
}

这篇关于出错发布到Web服务器后,将字符串转换为DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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