简体   繁体   English

无法将字符串转换为DateTime?

[英]Unable to convert a string to DateTime?

Inside a function, I need to find the difference between 2 dates in seconds . 在函数内部, 我需要找到2个日期(以秒为单位)之间的差异 If the difference is more than 30 seconds i return False otherwise it returns True , first one I read it from database and Second one is the current DateTime.Now 如果相差超过30秒,则返回False,否则返回True ,第一个是我从数据库中读取的,第二个是当前的DateTime.Now

Here is the snippest of code I'm using that does the work while dr.GetValue(0).ToString() holds the current value in the database : 这是我正在使用的最简洁的代码,而dr.GetValue(0).ToString()将当前值保存在数据库中:

if (dr.Read())
            {

                DateTime nowDate = Convert.ToDateTime(DateTime.Now.ToString("M/dd/yyyy H:mm:ss tt"));
                DateTime then = DateTime.ParseExact(dr.GetValue(0).ToString(), "M/dd/yyyy H:mm:ss tt", CultureInfo.InvariantCulture);

                TimeSpan diff = nowDate - then;

                int timeDifference = diff.Seconds;


                if (timeDifference > 30)
                {
                    myConn.Dispose();
                    return false;
                }
                else {
                    myConn.Dispose();
                    return true;
                }
            }

When i execute the code above i get a message error : 当我执行上面的代码时,出现消息错误:
string was not recognized as valid DateTime

And here is the line that is causing the error : 这是导致错误的行:

 DateTime then = DateTime.ParseExact(dr.GetValue(0).ToString(), "M/dd/yyyy H:mm:ss tt", CultureInfo.InvariantCulture);

The time is stored in the database in this format : 2013-02-18 14:06:37 时间以这种格式存储在数据库中: 2013-02-18 14:06:37

But when I execute the following line (for debugging purposes) : 但是,当我执行以下行(出于调试目的)时:

MessageBox.Show(dr.GetValue(0).ToString());

I see a message box that shows the date in this format : 2/18/2013 2:06:37 PM 我看到一个消息框,以以下格式显示日期: 2/18/2013 2:06:37 PM

How to find the difference in seconds between the current time and the time stored in dr.GetValue(0).ToString() 如何找到当前时间与存储在dr.GetValue(0).ToString()中的时间之间的秒差

Any help would be highly appreciated 任何帮助将不胜感激

You want h , not H . 您想要h ,而不是H h is the hour in 12-hour format, H is the hour in 24-hour format. h是12小时格式的小时, H是24小时格式的小时。 Since your example hour is 2 PM (not 14 PM ) it's the 12-hour format you want. 由于您的示例小时是2 PM (而不是14 PM ),因此它是您想要的12小时格式。

Also: 也:

  • You're converting your now-time to a string and back - don't bother! 您正在将当前时间转换为字符串并返回-不用担心!
  • You're counting Seconds not TotalSeconds - this is incorrect because eg a 60-second period gives a Seconds value of 0 . 您是在计算Seconds而不是TotalSeconds这是不正确的,因为例如60秒的周期将SecondsSeconds 0
DateTime nowDate = DateTime.Now;
DateTime then = DateTime.ParseExact(
    dr.GetValue(0).ToString(), "M/dd/yyyy h:mm:ss tt",
    CultureInfo.InvariantCulture);

TimeSpan diff = nowDate - then;

double secondsDifference = diff.TotalSeconds;

You should even be able to do something along the lines of 您甚至应该能够按照以下方式进行操作

DateTime then = dr.GetDateTime(0);

and avoid the string-parsing altogether, but the H / h difference is the reason you get the specific exception you asked about. 并完全避免使用字符串解析,但是H / h差异是您得到所要求的特定异常的原因。

if, as it looks like, your date is a datetime in the database, you can probably simplify the two lines to this: 如果看起来像您的日期是数据库中的日期时间,则可以将以下两行简化为:

DateTime nowDate = DateTime.Now;
DateTime then = (DateTime)dr.GetValue(0);

(although I'm making a lot of assumptions here) (尽管我在这里做了很多假设)

Your code really should be very simple: 您的代码确实应该非常简单:

if (dr.Read())
{
    DateTime then = dr.GetDateTime(0);
    TimeSpan diff = DateTime.Now - then;
    int timeDifference = diff.TotalSeconds;
}

One thing to note - you really shouldn't be calling myConn.Dispose(); 需要注意的一件事-您实际上不应该调用myConn.Dispose(); in your if/else . 在您的if/else Wrap your connection and readers in a using statement. using语句包装您的连接和读者。

I think your server has Application server has some other date format set. 我认为您的服务器已为Application Server设置了其他日期格式。 What you can try is this: 您可以尝试的是:

Convert.ToDate(value,System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormate);

Hope this will solve the error.Haven't tested it so hope for best 希望这能解决错误,没有测试过所以希望最好

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM