Let us continue with the Windows Phone 7.5 Mango Apps Development tutorial series! In our previous tutorial we looked at string manipulation and all the ways we can do it in the .NET Class library. You will often be working with date and time, so this lesson focuses on working with the DateTime class, working with Date Time math, formatting DateTime and a lot more.
So let’s get started!
Just as our previous lesson, we shall create a new project with a unique name. In the design window we shall have a Button in the left top corner and a TextBlock below it (See image).
Now let’s add some C# code for the Button’s Click event. Copy the following lines of code in your button’s click event.
DateTime myValue = DateTime.Now;
myTextBlock.Text = myValue.ToString();
The First line creates an object of the DateTime class called myValue. The object’s value is set to DateTime. Now so that it gets the value of date and time at this current instance. The Date and time value stored is based on the phone’s regional date and time settings. The Second line merely displays it in the TextBlock. Remember, the TextBlock. Text attribute is of String data type, hence we convert out DateTime value to String using the ToString() method. That’s as easy as it gets in working with basic DateTime.
Now what if you wanted to display only the Date and exclude the time? Luckily there are a number of methods available in the DateTime class which will enable you to display the date and/or time in a specific format. Let’s have a look at these methods and the output they generate.
1. myTextBlock.Text = myValue.ToShortDateString();
This statement displays only the Date in a short date format,i.e., dd/mm/yyyy or mm/dd/yyyy depending upon phone’s regional date time settings. E.g. 03/01/2012
2. myTextBlock.Text = myValue.ToShortTimeString();
In this statement we use the ToShortTimeString() method to display only the time. E.g. 1:06 PM
3. myTextBlock.Text = myValue.ToLongDateString();
In this statement the ToLongDateString() method displays the Day of the week followed by the Date in numbers, the Month in alphabets and the year in numbers. E.g. Thursday, March 01, 2012.
Now what if you wanted to display the date after subtracting 3 days from now? In order to carry our math on DateTime objects the DateTime class has a whole lot of methods. In our case we shall use the AddDays() method with a parameter value as -3. The result would be 02/27/2012 or 27/02/2012 depending upon the phone’s regional DateTime settings.
myTextBlock.Text = myValue.AddDays(-3).ToShortDateString();
If you had to add days then you would use a positive integer as a parameter value to the AddDays() method. Similarly you can perform math on the Time part of the DateTime by using methods like AddHours(), AddMinutes(), etc. Notice how we chain methods together.
myTextBlock.Text = myValue.AddDays(3).ToShortDateString();
myTextBlock.Text = myValue.AddHours(3).ToShortTimeString();
Moving on, suppose that you had to work with only a part of the date say you wanted to work with the month then you could do something like this
myTextBlock.Text = myValue.Month.ToString();
Notice that Month returns an integer value and hence we use the ToString() method to display it in the TextBlock. Similarly you can accesss a number of attributes like Day, Day of Week, Day of Year, Seconds, Minutes, hours, etc.
Now let’s take a look at a TimeSpan object. A TimeSpan is not a specific date but rather is the span of time between two dates or time. So if you wanted to know your age you could do something like this.
TimeSpan myAge = DateTime.Now.Subtract(myBirthday);
myTextBlock.Text = myAge.ToString();
The Subtract() method has a return type of TimeSpan type. You could use a combination of properties and methods to translate the output shown into the number of years, months, days, hours, minutes and seconds.
That’s it for this set of tutorial; you could find more information on DateTime and TimeSpan by visiting MSDN.
In our next tutorial, we will learn about understanding and creating Classes…till then … take care!