Skip to main content

C# Custom Datetime Format String

Many Date times format patterns are available in c# asp.net, you can use them and get custom format as you required, DateTime Type have the method ToString that can be use to get custom datetime format just pass the string format.

Below is the many letter format provided by Datetime type ToString Method and expected result given in small bracket definition is given in bold string after equal. 

Day

d , dd = Day (1, 01)
ddd , dddd = Day of week (Mon, Monday)

Month

M , MM = Month (2 , 02)
MMM , MMMM = Month Name Of The Year (Jan, January)

Year

y , yy = Year (13, 13)
yyy , yyyy = Year (2013, 2013)

Hours

h, hh = 12 Hours (2, 02)

24 Hours Format

H , HH = 24 Hour (2, 02)

Minutes and Seconds

m , mm = Minute (6, 06)
s, ss = Second (3, 03)

Time Type

t = A.M, P.M (A, P)
tt = A.M, P.M (AM, PM) 

About Single and double letter

dd , MM, yy, hh, HH, mm, ss = Display leading Zero
d , M, y, h, H, m, s = Do not display leading Zero

Example is given below, code and its output (in front of the code)

DateTime.Now.ToString("MM/dd/yyyy"); //04/13/2013
DateTime.Now.ToString("M/d/yyyy");   //4/13/201
DateTime.Now.ToString("ddd MMM yyyy"); //Sat Apr 2013
DateTime.Now.ToString("dddd - MMMM - yyyy"); //Saturday - April - 2013

DateTime.Now.ToString("dddd MMMM yyyy  hh:mm:ss tt"); //Saturday April 2013 03:07:29 PM

24 Hours Format :
DateTime.Now.ToString("dddd HH:mm:ss"); //Saturday 15:27:51


Here is the some predefined Single-letter datetime format provided by asp.net.

DateTime.Now.ToString("F"); //Saturday, April 13, 2013 3:16:54 PM
DateTime.Now.ToString("f"); //Saturday, April 13, 2013 3:13 PM
DateTime.Now.ToString("G"); //4/13/2013 3:16:19 PM
DateTime.Now.ToString("s");  //2013-04-13T15:19:22
DateTime.Now.ToString("u"); //2013-04-13 15:23:46Z
DateTime.Now.ToString("g"); //4/13/2013 3:15 PM

DateTime.Now.ToString("D"); //Saturday, April 13, 2013
DateTime.Now.ToString("d"); //4/13/2013
DateTime.Now.ToString("M"); //April 13
DateTime.Now.ToString("m"); //April 13
DateTime.Now.ToString("Y"); //April 13
DateTime.Now.ToString("y"); //April 13

DateTime.Now.ToString("t"); //3:20 PM
DateTime.Now.ToString("T"); //3:20:46 PM


Popular posts from this blog

How to validate dropdownlist in JavaScript

In this article you will see how to put validation in dropdownlist by javascript, suppose first item value of dropdownlist is 0 and text is "-Select-" just like given below and we have to validate that at least one item is selected excluding default i.e "-Select-".

regex - check if a string contains only alphabets c#

How to validate that input string contains only alphabets, validating that textbox contains only alphabets (letter), so here is some of the ways for doing such task. char have a property named isLetter which is for checking if character is a letter or not, or you can check by the regular expression  or you can validate your textbox through regular expression validator in asp.net. Following code demonstrating the various ways of implementation.

Regular expression for alphanumeric with space in asp.net c#

How to validate that string contains only alphanumeric value with some spacial character and with whitespace and how to validate that user can only input alphanumeric with given special character or space in a textbox (like name fields or remarks fields). In remarks fields we don't want that user can enter anything, user can only able to enter alphanumeric with white space and some spacial character like -,. etc if you allow. Some of regular expression given below for validating alphanumeric value only, alphanumeric with whitspace only and alphanumeric with whitespace and some special characters.

Remove blog title from Google's Search Results

By default, the google and lots of  search engines displays search results of  blog's  (Blogger)  in the following format as: BlogTitle:PostTitle For that reason our post doesn't come at top at the search result when any one exactly search as post title so we have to remove blog title from the search result of search engines.