C# Function to Get Current DateTime

Updated on 21 May 2012,
Published on 20 Feb 2008

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

Time Formats using ToString Function

Convert String to DateTime

Continue to next tutorial: C# Time Format using ToString Function to learn how to convert the Time value into specified Time format.

12 Responses to "C# Function to Get Current DateTime"
Binju
Thanks for the tips
ambarish singh
Thanks for help Sir..
Ashok dasari
This article is really nice.
It helped me alot.
I don't know about the DateTime object and its properties.
Thank you very much.
Ena
Wow - exaclty what I was looking for...
Thanks!
Mona Rahil
thank u so mush
it is very simple tutorial ,it helps me alot
Piggy
Thank you so much
suma
thanks to help us....
snehal
thnx alot... :)
charmi
Thanks for the tips
Chaitali
Thanks for the tips
swati
thank for the helps to C# program
chandan
Thanks
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers