Implementing DateTime Class in Windows Phone Application Using C#


This is a very small article in which we will implement the DateTime Class in Windows Phone Applications using C#.

With this class we can do many Date-Time related operations like calculating the time span between two dates and many more.

Here I am showing only the calculation of the time span between two dates. Using this concept you can use several functions of the DateTime class.


Step 1: Create a New “Windows Phone Application” in Visual Studio and name it as you choose (I named it CalCulateDate). Now a new Windows Phone Application Page (MainPage.xaml) will be generated.

Step 2: Now add two Button Controls and a TextBlock from the toolbox window to your project. The First Button Control is to show how to calculate between two dates and the second one simply shows the Current Date and Time with the specified format. Edit the content for the first button as “Calculate Date” and it's name as “buttonDate”. Similarly for the second button's content use “Current Date” and “buttonCurrDate” for it's name.

Step 3: Navigate to the Button Event handler for the Calculate Date button (buttonDate) and add the following code.
In the Button Event

//Creating the Instance for DateTime Class and

// Parsing the Date

DateTime myDate = DateTime.Parse("12/11/1993");

//Subtracting the Entered age with the Current

// Date

TimeSpan myAge = DateTime.Now.Subtract(myDate);

//Displaying the Value of the Calculated

textBlock1.Text = myAge.ToString(); 

We are just creating an instance of the DateTime Class and using the Parse method we are parsing the date of our choice (I am parsing here 12/11/1993).
Now it's time to calculate the TimeSpan, what we are doing is that we are subtracting our date that we have passed already from the current date and storing it into a variable of TimeSpan type.

Finally we are showing the result in the TextBlock in our project that is the total time span between the two dates.

Step 3: Now it's time to add code to display the current date and time with the specified format.

Navigate to the second button control in your project “Current Date”(buttonCurrDate) and add the following code:

//Specifying the Format in which the Output is to be Displayed

string dateFormat = @"d/M/yyyy hh:mm:ss tt";

//Creating the instance for DateTime and storing the

//Current Date and Time in it

DateTime myDate = DateTime.Now;

//Formatting the Date and time and Displaying in the

// TextBlock

string myDateString = myDate.ToString(dateFormat);

textBlock1.Text = myDateString;

Step 4: Now let us explore another property of the DateTime class that will provide us the name of the day of the week using the property “DayOfWeek”.

Add a button to your project and name it “Day Of Week” and just add the event of that button and add the following code to it:

//Creating the Instance for DateTime Class and Parsing the //Date

DateTime myDate = DateTime.Parse("12/03/2014");

//Showing the Day of the week of the specified date by //Using the Property "DayOfWeek"

textBlock1.Text = myDate.DayOfWeek.ToString();

That's all for this article. I am attaching the source file so that you can go through it.