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

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.

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-".

Uploading large file in chunks in Asp.net Mvc c# from Javascript ajax

Often we have a requirement to upload files in Asp.net, Mvc c# application but when it comes to uploading larger file, we always think how to do it as uploading large file in one go have many challenges like UI responsiveness, If network fluctuate for a moment in between then uploading task get breaks and user have to upload it again etc.

how $document.ready() is different from window.onload()

we often use window.onload() in javascript and $document.ready() in jquery and assume that both are same just jquery library wrap up javascript window.onload() and introduce $document.ready() but no there is much difference between them below is the explanation -