简体   繁体   English

如何比较DateTime值

[英]How to Compare DateTime Values

I am saving my app's first launch date plus three days from now as a string value. 我将应用程序的首次启动日期以及从现在起三天保存为字符串值。 I am then attempting to perform a string comparison when the MainPage is NavigatedTo to compare these string values. 然后,当MainPage被NavigatedTo比较这些字符串值时,我尝试执行字符串比较。 To note, this will have to work in any culture set, although from what I understand here http://msdn.microsoft.com/en-us/library/system.datetime.now(v=vs.110).aspx DateTime.Now will always get the local values for a correct comparison. 需要注意的是,这将必须在任何文化环境中都有效,尽管据我所知http://msdn.microsoft.com/en-us/library/system.datetime.now(v=vs.110).aspx DateTime.Now将始终获取本地值以进行正确的比较。 Currently I am getting a TargetInvocationException when doing this, and not sure how to solve the issue 目前,我在执行此操作时会收到TargetInvocationException ,但不确定如何解决该问题

Store this when the app is first launched into a custom Settings class which will save the string value 在应用程序首次启动到自定义Settings类中时存储此内容,该类将保存字符串值

Settings.FutureDate.Value = DateTime.Now.AddDays(3).ToString();

Check this in the app page when loaded 加载后在应用页面中检查此内容

DateTime currentDate = DateTime.Now;
DateTime futureDate = DateTime.Parse(Settings.FutueDate.Value); 

//If three days has passed, notify user
if (currentDate >= DateTime.Parse(Settings.FutureDate.Value)) //TargetInvocationException error occurs here
{
    MessageBox.Show("Three days has passed.");
}

EDIT 编辑

When checking the currentDate and Settings.FutureDate.Value values from above before the comparison, I see the following. 在比较之前从上方检查currentDateSettings.FutureDate.Value值时,看到以下内容。

currentDate = {8/13/2014 3:07:17 PM}
futureDate = {8/16/2014 3:07:09 PM}

I think you should store the date itself as a DateTime instead of using a string, if Settings.FutureDate is a Nullable<DateTime> then: 我认为您应该将日期本身存储为DateTime而不是使用字符串,如果Settings.FutureDateNullable<DateTime>则:

Settings.FutureDate= DateTime.Now.AddDays(3);

Then your code becomes: 然后您的代码变为:

DateTime currentDate = DateTime.Now;

if (Settings.FutureDate)
{
    //If three days has passed, notify user
    if (currentDate >= Settings.FutureDate.Value) 
    {
        MessageBox.Show("Three days has passed.");
    }
}

There is no reason to store the string rather than the DateTime itself as you can conveniently obtain the string at any time. 没有理由存储字符串,而不是存储日期时间本身,因为您可以随时方便地获取字符串。 This is likely to get rid of your error if it was due to creating the DateTime from a string . 如果这是由于从string创建DateTime引起的,则很可能会消除您的错误。

Please can you print the value of the string FutureDate.Value ? 请您可以打印字符串FutureDate.Value的值吗? Also try to split your code in the following manner and tell us what line causes the error: 另外,请尝试按以下方式拆分您的代码,并告诉我们导致错误的行:

DateTime currentDate = DateTime.Now;

DateTime settingsDate = DateTime.Parse(Settings.FutureDate.Value);
if (currentDate >= settingsDate) 
{        //If three days has passed, notify user
    if (currentDate >= Settings.FutureDate.Value) 
    {
        MessageBox.Show("Three days has passed.");
    }
}

I suspect the error will happen on the DateTime.Parse . 我怀疑该错误将发生在DateTime.Parse To know why we need you to print the value of Settings.FutureDate.Value . 要知道为什么我们需要您打印Settings.FutureDate.Value的值。

EDIT1: I saw your string, you did not say the type of Settings.FutureDate . EDIT1:我看到了您的字符串,您没有说出Settings.FutureDate的类型。 However this works for me in a fresh new project, please note the ParseExact where I provide the exact format of the string to parse. 但这对我来说是一个全新的项目,请注意在ParseExact ,我提供了要解析的字符串的确切格式。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Settings
    {


        public static string FutureDate { get; set; }

    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Settings.FutureDate = "8/16/2014 3:07:09 PM";
        }



        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DateTime currentDate = DateTime.Now;
            CultureInfo provider = CultureInfo.InvariantCulture;
            DateTime futureDate = DateTime.ParseExact(Settings.FutureDate, "M/d/yyyy h:mm:ss tt", provider);

            if (currentDate >= futureDate) //TargetInvocationException error occurs here
            {
                MessageBox.Show("Three days has passed.");
            }
        }
    }
}

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

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