Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串未被识别为有效的datetime asp.net c#_C#_Asp.net_Class_Datetime_Web Applications - Fatal编程技术网

字符串未被识别为有效的datetime asp.net c#

字符串未被识别为有效的datetime asp.net c#,c#,asp.net,class,datetime,web-applications,C#,Asp.net,Class,Datetime,Web Applications,当我遇到上述错误时,我尝试使用datetime journeydate=datetime.parse。。。。但这也会引发一个错误,比如字段初始值设定项不能引用非静态字段、方法或属性 public class aaa { public string created_date { get; set; } DateTime journeyDate = DateTime.ParseExact(created_date, "yyyy-MM-ddThh:mm:ss", CultureInfo

当我遇到上述错误时,我尝试使用datetime journeydate=datetime.parse。。。。但这也会引发一个错误,比如字段初始值设定项不能引用非静态字段、方法或属性

public class aaa
{
    public string created_date { get; set; }
    DateTime journeyDate = DateTime.ParseExact(created_date, "yyyy-MM-ddThh:mm:ss", CultureInfo.InvariantCulture); // error
}

此创建日期将直接从数据库检索。。是否有任何可能的方法来避免字符串未被识别的错误?

您的日期格式甚至与您输入的日期格式不符。你至少应该注意到自己今年的职位不匹配

public class aaa
{
    public string created_date { get; set; }
    //This code "runs" so to say. You can't put that in a class. 
    //DateTime journeyDate = DateTime.ParseExact(created_date, "yyyy-MM-ddThh:mm:ss", CultureInfo.InvariantCulture);

    //Can't do this either, because when will it be called? "runable" code needs to be in a method.
    //for(int i = 0; i < created_date.length; i++){

    //We can however only decalre journeyDate
    private DateTime journeyDate;

    //And then use either a method or constructor to set it:

    public void InitializeJourneyDate()
    {
        journeyDate = DateTime.ParseExact(created_date, "yyyy-MM-ddThh:mm:ss", CultureInfo.InvariantCulture);
    }

    public aaa()
    {
        journeyDate = DateTime.ParseExact(created_date, "yyyy-MM-ddThh:mm:ss", CultureInfo.InvariantCulture);
    }

    //Maybe the best method is to use a property getter. This will return a diffrent datetime automatically when you change your string

    private DateTime JourneyDateProp
    {
        get
        {
            return DateTime.ParseExact(created_date, "yyyy-MM-ddThh:mm:ss", CultureInfo.InvariantCulture);
        }
    }

    public void Convert()
    {
        List<string> dateTimeStrings = new List<string>(){
            "2018-01-19T09:10:52",
            "2018-01-22T09:10:52",
            "2018-01-28T09:10:52"
        };

        List<DateTime> dateTimes = new List<DateTime>();

        foreach(string s in dateTimeStrings)
        {
            dateTimes.Add(DateTime.ParseExact(created_date, "yyyy-MM-ddThh:mm:ss", CultureInfo.InvariantCulture));
        }
    }
}
公共级aaa
{
创建的公共字符串\u日期{get;set;}
//这段代码可以说是“运行”的。你不能把它放在一个类中。
//DateTime journeyDate=DateTime.ParseExact(创建日期,“yyyy-MM-ddThh:MM:ss”,CultureInfo.InvariantCulture);
//也不能这样做,因为什么时候调用它?“可运行”代码需要在方法中。
//for(int i=0;i
首先,请提供一个完整的示例。您显示的代码甚至无法正确编译,因为所有代码都必须在一个方法中。接下来,当您获得异常时,
created\u date
的值是多少?如果数据库为该列返回空值,则解析将失败。每当日期不是f时,解析也会失败ORMATED正确。created_date的值为2018-01-19T09:10:52。该字符串不符合格式,这里的问题是什么?您已经明确表示格式为
dd/MM/yyyyy h:MM tt
,然后传入一个包含
yyyy-MM的日期时间ddTHH:MM:ss
ParseExact
将因此引发这个异常。很好,这是我的错误。现在试着解决我的问题,而不是错误。它仍然显示编码时红线@my variable created_date错误--CS0236字段初始值设定项不能引用非静态字段、方法或属性“aaa.created_date”。它显示编码时红线@my variable created_date错误--CS0236字段初始值设定项无法引用非静态字段、方法或属性“aaa.created_date”,这是一个单独的问题。下面的代码运行,这就是您的问题所在。
static void Main(string[]args){string dateString=“2018-01-19T09:10:52”;DateTime testdate=DateTime.ParseExact(dateString,“yyyy-MM-ddThh:MM:ss”,CultureInfo.InvariantCulture);}
。您正在尝试在类中运行代码。如果您将解析代码移动到构造函数,它将停止抱怨。是的,谢谢..如果它是list=“2018-01-19T09:10:52”,“018-01-22T09:10:52”,“018-01-20T09:10:52”我可以遵循创建类的相同过程吗?不确定您的意思。如果是字符串列表,则DateTime属性将不起作用,除非您指定该列表中要使用的字符串的索引。确定,例如,我有list={2018-01-19T09:10:522018-01-22T09:10:522018-01-28T09:10:52}我需要将其转换为列表,因为当我使用列表执行时,它会抛出一个错误。所以最初将其作为字符串提及,并在下一步将其转换为日期时间。