Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment




Latest Articles


Move to top
Advanced Datetime Format for string
Posted By Sarin on Apr 26, 2012     RSS Feeds     Latest Hinduism news
1879 Views

Datetime Custom Formats
C# not only has the ability to display built-in DateTime formats but also display custom formats using DateTime format components. A component is a string that represents a certain part a date. For example, M represents a month number (1-12) and H represents an hour in 24 hour format.  
  


For the complete list, check the table below:
  
Suppose if the date is declared as  
DateTime date = new DateTime(2011, 8, 7,9,30,15); (year,month,day,hours,minutes,seconds)
  
 Then the result of each of custom date format specifier is  
  
Format  CharacterDisplayDescription
d 7 Single digit representation of the day of the month
dd 07 Double digit representation of the day of the month
ddd Sun Short Abbreviation of the day of the week
dddd Sunday Day of the week
M 8 display one-digit month number
MM 08 display two-digit month number
MMM Aug Short Abbreviation of the Month
MMMM August Month in full form
y 1 Last digit of the year
yy 11 Last two digits of the year
yyyy 2011 Full year from (0001-9999)
f sunday, august 07, 2011 9:30 AM                       Full date short time
F sunday, august 07, 2011 9:30:15 AM                       Full Date Full Time
g 8/7/2011 9:30 AM           General Date Short Time
gg A.D. Show current Era
G 8/7/2011 9:30:15 AM           General Date Long Time
o 2011-08-07T09:30:15.0000000 Represent time zone information
O 2011-08-07T09:30:15.0000000 Represent time zone information
h       9 display one-digit hour on 12-hour scale
H 9 Hour(12 hour format) no zero in front
Hh 09 Two Digit hour (12 hour format). Zero in front
HH 09 Hour (24 hour format)
m 3 minute with no zero in front
mm 30 minute with zero in front
s 1 second with no zero in front
ss 15 second with zero in front
t 9:30 AM Short Time(No Seconds) with AM/PM
T 9:30:15 AM Full Time(With Seconds) with AM/PM
tt AM A = AM, P = PM
u 2011-08-07 09:30:15Z Universal Sortable Time Format
U sunday, august 07, 2011 6:30:15 AM                       Universal Full Time Format
f 0                       represents the tenths of a second in a date and time value
ff 02 Represents the hundredths of a second in a date and time value.
fff 022 Represents the milliseconds in a date and time value.
ffff 0220 represents the ten thousandths of a second in a date and time value
fffff 02200 represents the hundred thousandths of a second in a date and time value
ffffff 022000 represents the millionths of a second in a date and time value
F 0 represents the tenths of a second in a date and time value
FF 02 represents the hundredths of a second in a date and time value
FFF 022 represents the milliseconds in a date and time value
FFFF 022 represents the ten thousandths of a second in a date and time value
FFFFF 022 represents the hundred thousandths of a second in a date and time value
FFFFFF 022 represents the millionths of a second in a date and time value
z +3 Single digit representation of time zone offset
zz +03 double digit representation of  time zone offset
zzz +03:00 Representation of time zone offset in hours and minutes
: 6:30:15The time separator
/ or - 8/7/2011 or 8-7-2011The date separator depending upon culture settings

Below are some of the examples displaying combination of all above format specifier
  
Console.WriteLine(date.ToString("d, M", DateTimeFormatInfo.InvariantInfo));
            // output: 7, 8
  
Console.WriteLine(date.ToString("d, MMMM", CultureInfo.CreateSpecificCulture("fr-FR")));
  // output: 7, aout

Console.WriteLine(date.ToString("dd,MM", CultureInfo.CreateSpecificCulture("sv-SE")));
  // output: 07,08
  
Console.WriteLine(date.ToString("dddd dd MMMM", CultureInfo.CreateSpecificCulture("nl-BE")));
// output: zondag 07 Augustus
  
Console.WriteLine(date.ToString("(M) MMM, MMMM", CultureInfo.CreateSpecificCulture("lv-LV")));
            // Displays (8) aug, Augusts
  
Console.WriteLine(date.ToString("hh:mm:ss.ff tt", CultureInfo.InvariantCulture));
            // Displays 09:30:15.02 AM                        
  
Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", date));
            // +3, +03, +03:00                  
  
  DateTimeOffset date2 = new DateTimeOffset(2011, 8, 7, 9, 15,30, new TimeSpan(6, 0, 0));
   
  Console.WriteLine(String.Format("{0:%z}, {0:zz}, {0:zzz}", date2));
            // +6, +06, +06:00  
  
  Console.WriteLine(date.ToString("hh:mm:ss.f", CultureInfo.CreateSpecificCulture("sv-FI")));
            // Displays 09:30:15.0
  
     Console.WriteLine(date.ToString("hh:mm:ss.F", CultureInfo.InvariantCulture));
            // Displays 09:30:15
  
  Console.WriteLine(date.ToString("hh:mm:ss.ff", CultureInfo.CreateSpecificCulture("sv-FI")));
            // Displays 09:30:15.02
  
  Console.WriteLine(date.ToString("hh:mm:ss.FFF", CultureInfo.CreateSpecificCulture("es-MX")));
            // Displays 09:30:15.022
  
Console.WriteLine(date.ToString("MM/dd/yyyy g", CultureInfo.InvariantCulture));
            // Displays 08/07/2011 A.D.
                       
Console.WriteLine(date.ToString("h:m:s.F t", CultureInfo.InvariantCulture));
            // Displays 9:30:15 A                      
  
Console.WriteLine(date.ToString("hh:mm:ss tt", CultureInfo.CreateSpecificCulture("hu-HU")));
            // Displays 09:30:15 de  
                 
Console.WriteLine(date.ToString("H:mm:ss", CultureInfo.InvariantCulture));
            // Displays 09:30:15
 


here

Share this to your friends. One of your friend is waiting for your share.

Related Articles
Advanced Datetime Format for string
Symbolism of story behind kumbh mela-world largest gathering on earth
SQL-Removing numbers in a string
Tips and tricks with ajax calendar
Call Codebehind method from GridView ItemTemplate Eval function
Data Validation using Ajax MaskedEditExtender control
Capitalise first character of every word
Create property class of database table
Who invented snakes and ladders
Ram Setu samudram project-the disaster project of all times

Post Comment




Software developer by profession,
Poet by hobby,
Blogger by Passion
Birth Place: Munger, Bihar
Residence: Mumbai, Maharashtra
Prof exp: 8 years

Read More about me

Read my daily what'sapp quotes
Most Viewed Articles

Latest Spiritual Videos


Latest Funny Videos

Latest News

Latest Social Post
Latest WhatsApp Quotes