DateTime.Now in C#

DateTime Class is used to get the various possibilities that can be included within a day, month or a year. I am going to discuss about some functionalities of DateTime.Now here.

For example:
To get the current date and time, use DateTime.Now which gives us 6/24/2009 9:45:00 AM

DateTime.Now has 5 important functions to get date and time in different formats.

1) DateTime.Now.ToLongDateString() - Displays in the format "Wednesday, June 24, 2009."
2) DateTime.Now.ToLongTimeString() - Displays in the format "12:00:00 AM. "
3) DateTime.Now.ToShortDateString() - Displays in the format "6/24/2009"
4) DateTime.Now.ToShortTimeString() - Displays in the format "12:00 AM"
5) DateTime.Now.ToString("Format") - This method accepts different formats. The formats can be obtained from GetDateTimeFormats() method of DateTime.Now and has 133 different formats that anyone can think of while displaying Date and Time.
For example: DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt")
This would display the Current Date Time in the form : 06/24/2009 9:43:23 AM

Note the difference between "MM" and "mm" which are used for Month and minutes respectively.
tt is used to display as AM or PM.

Hope this is useful.

Thanks,
Sree

Comments

  1. Simple topic
    but handy and well explained
    Thanks, I'll be following you

    ReplyDelete
  2. Yes just wanted to start off with a simple topic to boost myself. Thanks for the comment.

    ReplyDelete

Post a Comment

Popular posts from this blog

Knowing too much is bad(in the world of OOP)

Reverse a LinkedList Algorithm