Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • DateTime To string format

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 180
    Comment on it

    In C#, we use a method DateTime.ToString(string) to get the value of a DateTime in specific pattern of string.

     

    Syntax:-

    public string ToString( string format )

     

    It accepts one parameter "format" which specifies the date and time string format pattern. If the user didn't specify the value of this parameter then it will use a general format.

    This function returns a string value of DateTime variable as specified in format variable.

     

    Exceptions thrown by this method are as follows:

    1. FormatException ----------> This exception occurs if the length of format variable is 1 or if the format variable does not contain a valid format pattern.

    2. ArgumentOutOfRangeException -------> It occurs when the range of date and time is not supported by the calendar.

     

    C# Code

    using System;
    
    class DateTimeProgram
    {
       static void Main()
        {
           DateTime dt = new DateTime(2016, 5, 16);
           Console.WriteLine(dt);	
        }
    }

     

    Output

    August 15

     

    C# code to display the month in numeric format

    using System;
    
    class MProgram
    {
       static void Main()
        {
           DateTime mn = new DateTime(2016, 5, 16);
           Console.WriteLine(mn.ToString("MM"));
        }
    }

     

    Output

    05

     

    C# code to display the month in string format

    using System;
    
    class MProgram
    {
       static void Main()
        {
           DateTime ms = new DateTime(2016, 5, 16);
           Console.WriteLine(ms.ToString("MMMM"));
        }
    }

     

    Output

    May

     

    DateTime.ToString() Patterns:

    1. dddd, dd MMMM yyyy ----------> Monday, 16 May 2016

    2. dddd, dd MMMM yyyy ----------> HH:mm Monday, 16 May 2016 12:20

    3. dddd, dd MMMM yyyy ----------> hh:mm tt Monday, 16May 2016 12:30 PM

    4. dddd, dd MMMM yyyy ----------> H:mm Monday, 16 May 2016 12:35PM

    5. dddd, dd MMMM yyyy ----------> hh:mm tt Monday, 16 May 2016 12:40 PM

    6. dddd, dd MMMM yyyy HH:mm:ss ------> Monday, 16 May 2016 12:45:09

    7. MM/dd/yyyy HH:mm --------------> 05/16/2016 12:50

    8. MM/dd/yyyy hh:mm tt -------------> 05/16/2016 12:52 PM

    9. MM/dd/yyyy H:mm ---------------> 05/16/2016 12:55

    10. DateTime.ToString ( 'G' ) :

    MM/dd/yyyy HH:mm:ss ------------> 05/16/2016 01:03:35

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: