简体   繁体   English

字符串未被识别为有效日期时间asp.net C#

[英]string was not recognized as a valid datetime asp.net c#

when i got the above error i tried with datetime journeydate=datetime.parse.... but that also throws a error like a field initializer cant reference the non static field, method or property. 当我遇到上述错误时,我尝试使用datetime tripdatedate = 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
}

This created_date will be retrived directly from db.. Is there any possible way to avoid the string was not recognized error? 此created_date将直接从db中检索。是否有任何可能的方法来避免字符串未被识别错误?

Your date format is not even close to what you throw in there. 您的日期格式甚至与您输入的日期格式不符。 You could have at least noticed yourself that the position of year doesn't match. 您可能至少已经注意到自己年份的位置不匹配。

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));
        }
    }
}

暂无
暂无

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

相关问题 将字符串解析为DateType ASP.NET C#异常-无法将该字符串识别为有效的DateTime - Parse string to DateType ASP.NET C# exception - The string was not recognized as a valid DateTime 在asp.net中无法将字符串识别为有效的DateTime - String was not recognized as a valid DateTime in asp.net “在Asp.net中,字符串未被识别为有效的DateTime - "String was not recognized as a valid DateTime in Asp.net 字符串未被识别为有效的日期时间,asp.net - String was not recognized as a valid DateTime, asp.net 错误:字符串未被识别为有效的DateTime。 我正在使用带有asp.net和C#Web窗体和SQL数据库的jQuery Datepicker - Error: String was not recognized as a valid DateTime. I'm using jquery datepicker with asp.net and c# web forms and sql databse ASP.NET控制器FormatException:字符串未被识别为有效的DateTime - ASP.NET Controller FormatException: String was not recognized as a valid DateTime 在asp.net中,不能将字符串识别为有效的DateTime,托管在godaddy上 - String was not recognized as a valid DateTime in asp.net, hosted on godaddy 发布后出错:字符串未被识别为有效的日期时间 asp.net - Error after publishing : String was not recognized as a valid DateTime asp.net Javascript日期到ASP.NET日期:字符串未被识别为有效的DateTime - Javascript date to ASP.NET date: String was not recognized as a valid DateTime 无法将ASP.NET MVC字符串识别为有效的DateTime - ASP.NET MVC String was not recognized as a valid DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM