Matthew
Matthew

Reputation: 4056

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. 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. Currently I am getting a TargetInvocationException when doing this, and not sure how to solve the issue

Store this when the app is first launched into a custom Settings class which will save the string value

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.

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

Upvotes: 0

Views: 132

Answers (1)

BlueTrin
BlueTrin

Reputation: 10113

I think you should store the date itself as a DateTime instead of using a string, if Settings.FutureDate is a Nullable<DateTime> then:

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.

Please can you print the value of the string 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. To know why we need you to print the value of Settings.FutureDate.Value.

EDIT1: I saw your string, you did not say the type of 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.

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

Upvotes: 2

Related Questions