DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

C#.NET : DateTime.Now Month output format

By Ashok Nalam on 11 Mar 2014 | Category: C# | Tagged: datetime .NET number 
The snippet shows how to print double digit number for DateTime.Now.Month and also different .ToString() formats supported by month.
  Discuss Add Comment   |  
Add rating Rate this resource   

If current month is March and DateTime.Now.Month.ToString() returns 3. If you would like to get double digit format(03) instead of 3, use below code

DateTime.Now.ToString("MM") 
(or)
DateTime.Now.Month.ToString("d2")
We will see an example for some other formats supported by month.

namespace DotNetMirror
{
    class DateTimeNowMonthFormatDemo
    {
        public static void Main()
        {
            //print double digit
            Console.WriteLine(DateTime.Now.ToString("MM"));
            Console.WriteLine(DateTime.Now.Month.ToString("d2"));
            
            //other formats
            DateTime dt = Convert.ToDateTime("13-9-2014"); //13th September 2014
            Console.WriteLine(dt.ToString("M"));
            Console.WriteLine(dt.ToString("MM"));
            Console.WriteLine(dt.ToString("MMM"));
            Console.WriteLine(dt.ToString("MMMM"));
            Console.Read();
        }

    }
}
Output:
DateTime.Now.Month formats Example

  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 9526  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (2)
 
1. By ZAP on 23 Oct 2023

 
2. By ZAP on 23 Oct 2023

 
cheap jordans|wholesale air max|wholesale jordans|wholesale jewelry|wholesale jerseys