【问题标题】:string was not recognized as a valid datetime asp.net c#字符串未被识别为有效的日期时间 asp.net c#
【发布时间】:2018-06-27 13:59:02
【问题描述】:

当我遇到上述错误时,我尝试使用 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
}

这个 created_date 将直接从 db 中检索出来。有没有什么办法可以避免出现字符串未识别错误?

【问题讨论】:

  • 首先,请提供一个完整的例子。您显示的代码甚至无法正确编译,因为所有代码都必须在方法内。接下来,当你得到异常时,created_date 的值是多少?如果数据库为该列返回 NULL 值,则解析将失败。
  • 只要日期格式不正确,它也会失败。
  • created_date 的值为 2018-01-19T09:10:52。
  • 而且那个字符串不符合格式,这里的问题到底是什么?您已经明确表示格式为dd/MM/yyyy h:MM tt,然后传入包含yyyy-MM-ddTHH:mm:ss 的日期时间。 ParseExact 将因此抛出该异常。
  • 好吧我的错误..现在尝试解决我的问题而不是错误..在编码时它仍然显示红线@我的变量 created_date 错误-- CS0236 字段初始化程序无法引用非静态字段、方法或属性“aaa.created_date”。

标签: c# asp.net class datetime web-applications


【解决方案1】:

您的日期格式甚至与您输入的格式不一样。您至少可以注意到自己年份的位置不匹配。

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

【讨论】:

  • 它在编码时用红线@我的变量 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); }。您正在尝试在类中运行代码。如果您将解析代码移动到构造函数,它将停止抱怨。
  • 是的,谢谢.. 如果它是一个列表 = "2018-01-19T09:10:52","018-01-22T09:10:52"," 018-01-20T09:10:52" 我可以按照相同的程序创建课程吗?
  • 不确定你的意思。如果它是一个字符串列表,除非您指定要使用该列表中哪个字符串的索引,否则您的 DateTime 属性将不起作用。
  • 好的,例如,我有 list = {2018-01-19T09:10:52, 2018-01-22T09:10:52, 2018-01-28T09:10:52我需要将其转换为 list 因为当我使用 list 执行时,它会引发错误。所以最初将其作为字符串提及,并在下一步将其转换为日期时间。
猜你喜欢
  • 2011-10-21
  • 2015-09-30
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
相关资源
最近更新 更多