During the development of project you should come across the date manipulation and displaying specific format of the date or time to the user depending on the situation. I have also come across this situation where I have to display the time format in 12 hours and need to show it in Am/Pm format. Here in this short post I will try to summarize the maximum number of date format which I have used in my project, hope some of them may be helpful to you as well. First here are the predefined string functions which are used to get the date and time.
Date/Time Format | Output |
---|---|
DateTime.Now.ToLongDateString() | Monday, May 25, 2009 |
DateTime.Now.ToShortDateString() | 5/25/2009 |
DateTime.Now.ToLongTimeString() | 8:47:57 PM |
DateTime.Now.ToShortTimeString() | 8:47 PM |
DateTime.Now.ToString() | 5/25/2009 8:47:57 PM |
Now I will list down some the custom string format to get the date. In the below date format I have use the (/) backslash as separator, you can use separator like -(minus) sign as well. In the format below I have started with month/day/year, You can change it to day/month/year, or year/month/day etc.In the blow format as you can see that when I have placed 2 characters like dd the output is in the format of digit for the month and day case not in case of year. And in case of three characters the output will be in character case like Mon for the day and May for the month, and last is the case of the four character where the day is displayed in full name like in this Monday and In case of month which is three character May, it will display full name of month.
Date Format | Output |
---|---|
DateTime.Now.ToString("MM/dd/yyyy") | 05/25/2009 |
DateTime.Now.ToString("MM/ddd/yyyy") | 05/Mon/2009 |
DateTime.Now.ToString("MM/dddd/yyyy") | 05/Monday/2009 |
DateTime.Now.ToString("MMM/ddd/yyyy") | May/Mon/2009 |
DateTime.Now.ToString("MMMM/ddd/yyyy") | May/Mon/2009 |
DateTime.Now.ToString("MMMM/ddd/yy") | May/Mon/09 |
Here are the sum of the time format which may helpful to you. Here HH combination or single H is used for the Hours and MM or M is used for minutes and ss or s is used for the seconds portion and the last one which I really forget whenever I have to show time in 12 hours is the tt combination which is used to show time in Am/Pm format.
TimeFormat | Output |
---|---|
DateTime.Now.ToString("HH:MM") | 22:01 |
DateTime.Now.ToString("HH:MM:ss") | 22:05:49 |
DateTime.Now.ToString("hh:mm:ss") | 10:01:49 |
DateTime.Now.ToString("hh:mm:ss tt") | 10:01:49 PM |
DateTime.Now.ToString("hh:mm tt") | 10:01 PM |
DateTime.Now.ToString("H:m") | 22:2 |
Hope you will get some of the date and time format and get some sort of understanding of how you can use them to get the required output regarding the date and time. If you have any suggestion please share it withe me.
All and any comments / bugs / suggestions are welcomed!
All and any comments / bugs / suggestions are welcomed!
No comments:
Post a Comment