C# Function to Get Current DateTime
In C# ASP.Net 2.0 you can use DateTime Structure to get current date and time of the day. You can also customize the format of Date or Time according to your requirement.
Example
Different types of DateTime formats that can be displayed using C# are:
2/19/2008 1:19:44 PM
13:19:44.4246250
1:19 PM
13:19 44 PM
2/19/2008
02-19-2008
Feb 19, 2008
Tue 19 Feb, 2008
Tuesday 19 February, 2008
DateTime Format Examples:
You can see the live samples and examples of C# DateTime format from the following links:
To display the current DateTime, you can use the following C# Functions:
C# Date Formats
Example 1
Response.Write(DateTime.Now);
Output
2/19/2008 1:19:44 PM
Example 2
Response.Write(DateTime.Now.ToShortDateString());
Output
2/19/2008
Example 3
Response.Write(DateTime.Now.ToString("MM-dd-yyyy"));
Output
02-19-2008
Example 4
Response.Write(DateTime.Now.ToString("MMM dd, yyyy"));
Output
Feb 19, 2008
Note: You can use MMM to display the first 3 letters of Month name.
Example 5
Response.Write(DateTime.Now.ToString("ddd dd MMM, yyyy"));
Output
Tue 19 Feb, 2008
Note: You can use ddd to display the first 3 letters of name of the day such as Tue, Wed etc. Also to display the current date you can use dd.
Example 6
Response.Write(DateTime.Now.ToString("dddd dd MMMM, yyyy"));
Output
Tuesday 19 February, 2008
Note: You can use dddd to display the full name of the day such as Tuesday, Wednesday etc. and MMMM to display the full name of the Month.
Output:
You can see the outputs of above C# code from the following links:
Date Formats using ToString Function
Continue to next tutorial: C# Time Format using ToString Function to learn how to convert the Time value into specified Time format.
