请帮助字符串未被识别为有效的DateTime ERR? [英] Please help String was not recognized as a valid DateTime ERR?

查看:106
本文介绍了请帮助字符串未被识别为有效的DateTime ERR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.FormatException:String未被识别为有效的DateTime。



源错误:

第58行:apptime.SlotTime = DateTime.ParseExact(tempdatetime,dd / MM / yyyy HH:mm:ss tt,culture);





但它是当我将此代码复制到我的本地系统时工作,当我从服务器运行此应用程序时发生错误...请帮助我

System.FormatException: String was not recognized as a valid DateTime.

Source Error:
Line 58: apptime.SlotTime = DateTime.ParseExact(tempdatetime, "dd/MM/yyyy HH:mm:ss tt", culture);


"But it's work when i copy this code to to my local system" ,Error occurred while I running this application from server...pls help me

推荐答案

你应该使用TryParseExact来检查tempdatetime值的有效性,参见示例:

You should use TryParseExact to check the validity of the tempdatetime value, see example:
using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {

        string tempdatetime = "01/04/2014 01:00:00 AM";
        string format = "dd/MM/yyyy HH:mm:ss tt";
        DateTime dateTime;
        if (DateTime.TryParseExact(tempdatetime, format, CultureInfo.InvariantCulture,
        DateTimeStyles.None, out dateTime))
        {
            Console.WriteLine(dateTime);

        }
        else
        {
            Console.WriteLine("Not a valid date time");
        }
    }
}


您告诉系统确切的格式是什么,并且提供的数据不是以这种格式。

现在,这里有两种可能:首先,字符串不是dd / MM / yyyy HH:mm:ss tt格式 - 它可以输入为1 / 04/2014 08:09:10 AM由于dd部分只提供了一位数,因此不完全匹配。



第二个是 culture 是错误的:很可能您期望的文化与您的开发机器上的文化相匹配,并且生产服务器的配置方式也大不相同。这可能会导致同样的问题,但可能性要小得多。



从字符串开始:看看点亮并准确看到你要转换的内容 - 然后看看来自哪里!
You are telling the system exactly what format to expect, and the data supplied is not in that format.
Now, there are two possibilities here: firstly that the string is not in "dd/MM/yyyy HH:mm:ss tt" format - it may be entered as "1/04/2014 08:09:10 AM" which doesn't exactly match because the "dd" part has only been provided with a single digit.

The second is that the culture is wrong: It is quite possible that you are expecting a culture that matches that on your development machine, and the production server is configured very differently. This could cause the same problem, but is a lot less likely.

Start with the string: look at lit and see exactly what you are trying to convert - then look at where it comes from!


1.当您将应用程序运行到运行环境(如Web服务器)或数据来自数据库服务器时,当前文化信息使用该运行环境,这可能与您的本地计算机不同。



2.由于运行环境,在你的情况下, tempdatetime 的值不是格式你正试图转换(也许日期是英文格式,如:mm / dd / yyyy ......)。



3.如果您直接了解或间接(通过使用配置参数)运行环境的当前文化信息,您应该首先通过使用您的文化信息从字符串创建 DateTime 对象来修改代码运行环境(使用 DateTime.Parse(tempdatetime,serverCultureInfo); ),然后将生成的DateTime转换为自定义格式。
1.When you are running your application into an running environment, like a web server, or the data comes from a database server, the "current culture info" of that running environment is used and this could be different then your local computer.

2. Because of the running environment, in your case the value from tempdatetime is not in the format that your are trying to convert (maybe the date is in English format like: "mm/dd/yyyy...").

3.If you know directly or indirectly (by using configuration parameter) the "current culture info" of your running environment you should modify your code by first creating the DateTime object from string by using the culture info of your running environment (by using DateTime.Parse(tempdatetime, serverCultureInfo); ), and then convert the resulted DateTime into your custom format.


这篇关于请帮助字符串未被识别为有效的DateTime ERR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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