DateTime Format

DateTime Format In C#

6 min read

  • October 11,2024
  • Rajesh Goel

 

A date and time format string specifies how a DateTime or DateTimeOffset value is represented as text during a formatting operation. It can also determine how a date and time value should be formatted when parsing a string to convert it back into a date and time object. Custom format strings are made up of one or more custom date and time format specifiers. Any string that doesn’t follow the standard format is treated as a custom date and time format string.

A standard date and time format string consists of a single character that serves as the format specifier to define how a DateTime or DateTimeOffset value is displayed as text. If the format string contains more than one character, including spaces, it is considered a custom date and time format string.

Format

Output

DateTime.Now.ToString("MM/dd/yyyy")10/13/2024
DateTime.Now.ToString("dddd, dd MMMM yyyy HH:mm:ss")Sunday, 13 Oct 2024 11:54:06
DateTime.Now.ToString("MM/dd/yyyy HH:mm")10/13/2024 11:54
DateTime.Now.ToString("MM/dd/yyyy hh:mm tt")10/13/2024 11:54 AM
DateTime.Now.ToString("MM/dd/yyyy H:mm")10/13/2024 6:54
DateTime.Now.ToString("MM/dd/yyyy h:mm tt")10/13/2024 6:54 AM
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")10/13/2024 11:54:06
DateTime.Now.ToString("MMMM dd")Oct 13
DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK")2024-10-13T11:54:06.7199222-04:00
DateTime.Now.ToString("ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’")Sun, 13 Oct 2024 11:54:06 GMT
DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss")2024-10-13T11:54:06
DateTime.Now.ToString("HH:mm")11:54
DateTime.Now.ToString("hh:mm tt")11:54 AM
DateTime.Now.ToString("H:mm")6:54
DateTime.Now.ToString("h:mm tt")6:54 AM
DateTime.Now.ToString("HH:mm:ss")11:54:06
DateTime.Now.ToString("yyyy MMMM")2024 Oct

Definition of Format Specifier

Format Specifier

Definition

dThe day of the month, from 1 to 31
ddThe day of the month, from 01 to 31.
dddThe abbreviated name of the day of the week - Mon (en-US).
ddddThe full name of the day of the Week - Monday (en-US).
hhour, 12-hour clock from 1 to 12  2024-10-13T05:54:40 → 5
hhhour, 12-hour clock from 01 to 12  2024-10-13T05:54:40 → 05
Hhour, 24-hour clock from 01 to 12  2024-10-13T05:54:40 → 5
HHhour, 24-hour clock from 01 to 12  2024-10-13T13:54:40 → 13
mminute, from 0-59, 2024-10-13T13:09:40 → 9
mmminute, from 00-59, 2024-10-13T13:09:40 → 09
Mmonth, from 1-12, 2024-06-13T13:09:40 → 6
MMmonth, from 01-12, 2024-06-13T13:09:40 → 06
MMMabbreviated name of the month, 2024-10-13T13:54:40 → Oct
MMMMfull name of the month, 2024-10-13T13:54:40 → October
ssecond, from 0-59, 2024-10-13T13:54:06 → 6
sssecond, from 00-59, 2024-10-13T13:54:06 → 06
tfirst character of the AM/PM , 2024-10-13T13:54:06 → P
ttthe AM/PM, 2024-10-13T13:54:06 → PM
yyear, 0-99, 0001-10-13T13:54:06 → 1, 2024-10-13T13:54:06 → 24
yyyear, 00-99, 0001-10-13T13:54:06 → 01, 2024-10-13T13:54:06 → 24
yyyyear, minimum of 3 digits, 0001-10-13T13:54:06 → 001, 2024-10-13T13:54:06 → 2024
yyyyyear, 4 digit number, 0001-10-13T13:54:06 → 0001, 2024-10-13T13:54:06 → 2024
fThe tenths of a second in a date and time value  2024-10-13T14:54:40.7190000 → 7    2024-10-13T14:54:40.01 → 0
ffThe hundredths of a second in a date and time value  
2024-10-13T14:54:40.7190000 → 71 
2024-10-13T14:54:40.009 → 00
fffThe milliseconds in a date and time value  
2024-10-13T14:54:40.719 → 719   
2024-10-13T14:54:40.0005 → 000 
ffffThe ten-thousandths of a second in a date and time value  
2024-10-13T14:54:40.7195000 → 7195 
2024-10-13T14:54:40.0000500 → 0000  
fffffThe hundred-thousandths of a second in a date and time value  
2024-10-13T14:54:40.7195200 → 71952   
2024-10-13T14:54:40.0000050 → 00000  
ffffffThe millionths of a second in a date and time value  
2024-10-13T14:54:40.7195210 → 719521 
2024-10-13T14:54:40.0000005 → 000000
fffffffThe ten-millionths of a second in a date and time value  
2024-10-13T14:54:40.7195212 → 7195212 
2024-10-13T14:54:40.0001250 → 0001250
FIf non-zero, the tenths of a second in a date and time value  
2024-10-13T14:54:40.7190000 → 7    
2024-10-13T14:54:40.0100000 → (no output)   
FFIf non-zero, the hundredths of a second in a date and time value  
2024-10-13T14:54:40.7190000 → 71   
2024-10-13T14:54:40.0090000 → (no output) 
FFFIf non-zero, the milliseconds in a date and time value  
2024-10-13T14:54:40.719 → 719   
2024-10-13T14:54:40.0005000 → (no output) 
FFFFIf non-zero, the ten-thousandths of a second in a date and time value  
2024-10-13T14:54:40.7195000 → 7195 
2024-10-13T14:54:40.0000500 → (no output)   
FFFFFif non-zero, the hundred-thousandths of a second in a date and time value  
2024-10-13T14:54:40.7195200 → 71952   
2024-10-13T14:54:40.0000050 → (no output)    
FFFFFFIf non-zero, the millionths of a second in a date and time value  
2024-10-13T14:54:40.7195210 → 719521 
2024-10-13T14:54:40.0000005 → (no output)
FFFFFFFIf non- zero, the ten-millionths of a second in a date and time value  
2024-10-13T14:54:40.7195212 → 7195212 
2024-10-13T14:54:40.0001250 → 0001250

 

How Memetic Solutions Enhances User Experience

At Memetic Solutions, we use these notification permission concepts to keep applications user-friendly. By ensuring users control their settings, we create apps that are both intuitive and engaging, improving user satisfaction without disrupting their experience.