-4

Referring to one of my last questions. I need for my WPF application to Auto-Calculate the Age of the movie based off of the release date, and the current date. This is one area that I personally have no experience with. I would assume it would have something to do with DateTime and CurrentDate. Any suggestions?

My WPF has a Movie class which contains:

namespace FinalExam
{
    public class Movie
    {
        public string movieName { get; set; }

        public DateTime releaseDate { get; set; }

        public DateTime movieAge { get; set; }

        public string onDVD { get; set; }

        public string onBluRay { get; set; }

        public string genreType { get; set; }

        public string directorName { get; set; }

        public string producerName { get; set; }

        public int movieLength { get; set; }

        public string moveRating { get; set; }        
    }
}

MovieUtility class which contains:

namespace FinalExam
{
    public static class MovieUtility
    {
        public static bool isItDate(string dateString)
        {
            DateTime date;
            bool isDate;
            isDate = DateTime.TryParse(dateString, out date);
            return isDate;
        }
        public static DateTime ConvertStringToDate(string dateString)
        {
            DateTime date;
            DateTime.TryParse(dateString, out date);
            return date;
        }
    }
}

And the MainWindow.xaml which is where the user will input the data and a datagrid will display it, and last, the MainWindow.xaml.cs which is where all of the strings are called and the data will be added the the List.

namespace FinalExam
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //GLOBAL VARIABLE AREA
        List<Movie> movieList = new List<Movie>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void but_Submit_Click(object sender, RoutedEventArgs e)
        {
            bool isGoodToAddNewMovie = true;
            Movie newMovie = new Movie();
            newMovie.movieName = txtBox_MovieName.Text;
            newMovie.onDVD = txtBox_DVD.Text;
            newMovie.onBluRay = txtBox_BluRay.Text;
            newMovie.genreType = txtBox_Genre.Text;
            newMovie.directorName = txtBox_Director.Text;
            newMovie.producerName = txtBox_Producer.Text;
            newMovie.moveRating = txtBox_Rating.Text;

            if (MovieUtility.isItDate(txtBox_ReleaseDate.Text))
            {
                newMovie.releaseDate = MovieUtility.ConvertStringToDate(txtBox_ReleaseDate.Text);
                txtBox_ReleaseDate.Background = new SolidColorBrush(Colors.LightGray);
            }
            else
            {
                txtBox_ReleaseDate.Background = new SolidColorBrush(Colors.Red);
                isGoodToAddNewMovie = false;
            }

            int length;

            if (int.TryParse(txtBox_Length.Text, out length))
            {
                newMovie.movieLength = length;
                txtBox_Length.Background = new SolidColorBrush(Colors.LightGray);
            }
            else
            {
                txtBox_Length.Background = new SolidColorBrush(Colors.Red);
                MessageBox.Show("Please enter movie length in minutes.");
                isGoodToAddNewMovie = false;
            }

            //ADD PERSON TO LIST
            if (isGoodToAddNewMovie)
            {
                movieList.Add(newMovie);
                dataGrid_Movies.ItemsSource = new List<Movie>(movieList);
            }
        }
    }
}

So how would I go about Auto-Calculating the Age of the movie based on release date and have it automatically put the age of the movie into the DataGrid on the Window.

  • 3
    Have you done any research? Not difficult; try searching for "C# date difference" or similar. – ChrisC73 Dec 14 '15 at 04:56
  • 1
    This looks an awful lot like a FinalExam question... – gregstoll Dec 14 '15 at 04:57
  • I tried searching around for auto calculation of dates based on the current date but couldn't find any exact information on my predicament. I've been looking for the past 45 minutes. I'm just extremely new to C# and even more so with DateTime calculations. – Kevin Ware Dec 14 '15 at 04:58
  • Greg, our professor even told us to Google it. Another student asked the question how to go about using auto calculation. So I came to this awesome site that I've already learned so much on. – Kevin Ware Dec 14 '15 at 04:59
  • Like our system admin used to email us whenever we had a question - _"[read the manual](https://msdn.microsoft.com/en-us/library/1905yhe2(v=vs.110).aspx)"_ –  Dec 14 '15 at 05:01
  • If only I had a manual. I'd be all up in that. – Kevin Ware Dec 14 '15 at 05:05
  • Pretty sure there is at least one book on c# out there –  Dec 14 '15 at 05:08

2 Answers2

0

Make use of properties:

    private DateTime  _ReleaseDate;
    private double _AgeInDays;

    public DateTime  ReleaseDate
    {
        get { return _ReleaseDate; }
        set { _ReleaseDate = value;
        _AgeInDays = (DateTime.Now - value).TotalDays; 
        }
    }     
    //here we define a read-only property this will gives you the age in days
    //when ever you assign value to the  ReleaseDate.
    //You cannot directly assign value to AgeInDays. 
    public double AgeInDays
    {
        get { return _AgeInDays; }            
    }

Hence your movie class will be like this:

 public class Movie
        {
            public string movieName { get; set; }
            public string onDVD { get; set; }
            public string onBluRay { get; set; }
            public string genreType { get; set; }
            public string directorName { get; set; }
            public string producerName { get; set; }
            public int movieLength { get; set; }
            public string moveRating { get; set; }
            private DateTime _ReleaseDate;
            private double _AgeInDays;
            public DateTime ReleaseDate
            {
                get { return _ReleaseDate; }
                set
                {
                    _ReleaseDate = value;
                    _AgeInDays = (DateTime.Now - value).TotalDays;
                }
            }
            public double AgeInDays
            {
                get { return _AgeInDays; }
            }
        }
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • Shouldn't I add that in the class that contains all of the methods? I already have the Movie Age column showing up in the Datagrid I just needed the calculation in order to plop the correct date in that column. – Kevin Ware Dec 14 '15 at 05:02
  • include this in your `Movie` class and bind to the grid, it will gives you the output – sujith karivelil Dec 14 '15 at 05:05
  • I have it posted and binded. I exchanged releaseDate with _ReleaseDate and that's showing up in the grid, however, it's saying the Age in Days is 0. – Kevin Ware Dec 14 '15 at 05:21
0

This will get you the year difference:

Movie movie = new Movie()
TimeSpan ts = DateTime.Today - movie.releaseDate;
movie.movieAge = ts.Days/365;
Liren Yeo
  • 3,215
  • 2
  • 20
  • 41