Reputation: 501
I can't seem to get my head around this but what would be the best approach for displaying a list of upcoming events?
At the moment I have a list of json events being displayed. All these events have a specific date on them.
2016-04-01T13:15:00
Most of the events have passed so i do not need to have them shown. How would i go about only showing the upcoming events?
The main thing I cant figure out is how to get todays date...
This is the code I used to display the events.
async void PopulateTableWithData()
{
var service = new Data.RestService();
var response = await service.GetEventItemsAsync();
Debug.WriteLine("JSON RESULTS FOUND: " + response.Count + "events");
listView.ItemsSource = response; // Display data in listview
/**ITEM SELECTED EVENT**/
listView.ItemSelected += (sender, e) =>
{
var i = (listView.ItemsSource as List<EventItems>).IndexOf(e.SelectedItem as EventItems);
string title = response[i].Title;
string description = response[i].EventDescription;
string location = response[i].EventLocation;
string startTime = response[i].EventStartTime;
string endTime = response[i].EventEndTime;
string rowKey = response[i].RowKey;
Navigation.PushAsync(new EventsWebviewPage(title, description, location, startTime, endTime, ReplaceString(rowKey, " ", "%20")));
};
}
Would I have to use some form of DateTime?
Can anyone give me some guidance?
Thanks
EDITED
async void PopulateTableWithData()
{
var service = new Data.RestService();
var response = await service.GetEventItemsAsync();
Debug.WriteLine("JSON RESULTS FOUND: " + response.Count + "events");
//listView.ItemsSource = response; // Display data in listview
for(int i = 0; i < response.Count; i++)
{
// Split raw json date
string[] splitRawDateTime = response[i].EventEndTime.Split('T');
string[] splitRawDate = splitRawDateTime[0].Split('-');
string eventYear = splitRawDate[0];
string eventMonth = splitRawDate[1];
string eventDay = splitRawDate[2];
// Compare dates and display events according to date
DateTime currentDateTime = DateTime.Now;
DateTime eventDateTime = new DateTime(int.Parse(eventYear), int.Parse(eventMonth), int.Parse(eventDay), 0, 0, 0);
int compareDates = DateTime.Compare(eventDateTime, currentDateTime);
List<EventItems> upcomingEvents = new List<EventItems>();
if (compareDates < 0)
{
Debug.WriteLine("Event date {0} is earlier then current date {1}", eventDateTime, currentDateTime);
}
else
{
Debug.WriteLine("Event date {0} is later then current date {1}", eventDateTime, currentDateTime);
upcomingEvents.Add(response[i]);
}
listView.ItemsSource = upcomingEvents;
}
}
}
I have just tried the above code and it seems to be working but how do I only display the upcoming events?
I believe its an issue with my placement of
listView.ItemsSource = response;
I know im probably doing something wrong here. Can anyone help?
Upvotes: 0
Views: 56
Reputation: 596
You can get the current date and time by using DateTime.Now
You can also create a DateTime
object from a string representation of a date by using one of the DateTime.Parse()
functions. Then you can compare the two dates by using comparison operators: <, <=, >, >=, ==
Edit: You could try it his way:
async void PopulateTableWithData()
{
var service = new Data.RestService();
var response = await service.GetEventItemsAsync();
Debug.WriteLine("JSON RESULTS FOUND: " + response.Count + "events");
//listView.ItemsSource = response; // Display data in listview
//Instantiate the list of upcoming events before the loop
List<EventItems> upcomingEvents = new List<EventItems>();
for(int i = 0; i < response.Count; i++)
{
// Split raw json date
string[] splitRawDateTime = response[i].EventEndTime.Split('T');
string[] splitRawDate = splitRawDateTime[0].Split('-');
string eventYear = splitRawDate[0];
string eventMonth = splitRawDate[1];
string eventDay = splitRawDate[2];
// Compare dates and display events according to date
DateTime currentDateTime = DateTime.Now;
DateTime eventDateTime = new DateTime(int.Parse(eventYear), int.Parse(eventMonth), int.Parse(eventDay), 0, 0, 0);
int compareDates = DateTime.Compare(eventDateTime, currentDateTime);
if (compareDates < 0)
{
Debug.WriteLine("Event date {0} is earlier then current date {1}", eventDateTime, currentDateTime);
}
else
{
Debug.WriteLine("Event date {0} is later then current date {1}", eventDateTime, currentDateTime);
//Populate the list of upcoming events
upcomingEvents.Add(response[i]);
}
}
//Display the list of upcoming events
listView.ItemsSource = upcomingEvents;
}
Upvotes: 1