Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to print formatted date & time in C#

10 Answers

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = new DateTime(2015, 2, 18, 3, 40, 5);

            string s = dt.ToString("d");
            Console.WriteLine(s);   // 2/18/2015
        }
    }
}

/*
run:
 
2/18/2015

*/


answered Feb 18, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = new DateTime(2015, 2, 18, 3, 40, 5);

            Console.WriteLine("{0:d}", dt);  // 2/18/2015 // MM/dd/yyyy
        }
    }
}

/*
run:
 
2/18/2015

*/


answered Feb 18, 2015 by avibootz
edited Feb 18, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = new DateTime(2015, 2, 18, 3, 40, 5);

            Console.WriteLine("{0:D}", dt);  // Wednesday, February 18, 2015 // Dd MMMM yyyy
        }
    }
}

/*
run:
 
Wednesday, February 18, 2015

*/


answered Feb 18, 2015 by avibootz
edited Feb 18, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = new DateTime(2015, 2, 18, 3, 40, 5);

            Console.WriteLine("{0:f}", dt);  // Wednesday, February 18, 2015 3:40 AM 
                                             // Dd MMMM yyyy HH:mm
        }
    }
}

/*
run:
 
Wednesday, February 18, 2015 3:40 AM

*/


answered Feb 18, 2015 by avibootz
edited Jun 9, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = new DateTime(2015, 2, 18, 3, 40, 5);

            Console.WriteLine("{0:F}", dt); // Wednesday, February 18, 2015 3:40:05 AM 
                                            // Dd MMMM yyyy HH:mm:ss
        }
    }
}

/*
run:
 
Wednesday, February 18, 2015 3:40:05 AM

*/


answered Feb 18, 2015 by avibootz
edited Jun 9, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine(String.Format("{0:d/M/yyyy HH:mm:ss}", DateTime.Now));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:

9/6/2015 18:41:35
 
*/

 



answered Jun 9, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine(DateTime.Now.ToString("MMM ddd yyyy - HH:mm"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:

Jun Tue 2015 - 18:59
 
*/

 



answered Jun 9, 2015 by avibootz
edited Jun 9, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine(DateTime.Now.ToString("M/d/yy - h:mm"));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:

6/9/15 - 7:00
 
*/

 



answered Jun 9, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                DateTime now = DateTime.Now;

                Console.WriteLine(now.ToString("d")); // 6/9/2015
                Console.WriteLine(now.ToString("D")); // Tuesday, June 09, 2015
                Console.WriteLine(now.ToString("f")); // Tuesday, June 09, 2015 7:04 PM
                Console.WriteLine(now.ToString("F")); // Tuesday, June 09, 2015 7:04:07 PM
                Console.WriteLine(now.ToString("g")); // 6/9/2015 7:04 PM
                Console.WriteLine(now.ToString("G")); // 6/9/2015 7:04:07 PM
                Console.WriteLine(now.ToString("m")); // June 09
                Console.WriteLine(now.ToString("o")); // 2015-06-09T19:04:07.5311601+03:00
                Console.WriteLine(now.ToString("s")); // 2015-06-09T19:04:07
                Console.WriteLine(now.ToString("t")); // 7:04 PM
                Console.WriteLine(now.ToString("T")); // 7:04:07 PM
                Console.WriteLine(now.ToString("u")); // 2015-06-09 19:04:07Z
                Console.WriteLine(now.ToString("U")); // Tuesday, June 09, 2015 4:04:07 PM
                Console.WriteLine(now.ToString("y")); // June, 2015
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:

6/9/2015
Tuesday, June 09, 2015
Tuesday, June 09, 2015 7:04 PM
Tuesday, June 09, 2015 7:04:07 PM
6/9/2015 7:04 PM
6/9/2015 7:04:07 PM
June 09
2015-06-09T19:04:07.5311601+03:00
2015-06-09T19:04:07
7:04 PM
7:04:07 PM
2015-06-09 19:04:07Z
Tuesday, June 09, 2015 4:04:07 PM
June, 2015
 
*/

 



answered Jun 9, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                DateTime now = DateTime.Now;

                Console.WriteLine(now.ToLongDateString());  // Tuesday, June 09, 2015
                Console.WriteLine(now.ToLongTimeString());  // 7:07:45 PM
                Console.WriteLine(now.ToShortDateString()); // 6/9/2015
                Console.WriteLine(now.ToShortTimeString()); // 7:07 PM
                Console.WriteLine(now.ToString());          // 6/9/2015 7:07:45 PM
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:

Tuesday, June 09, 2015
7:07:45 PM
6/9/2015
7:07 PM
6/9/2015 7:07:45 PM
 
*/

 



answered Jun 9, 2015 by avibootz

Related questions

1 answer 201 views
201 views asked Sep 7, 2014 by avibootz
1 answer 139 views
2 answers 230 views
1 answer 118 views
...