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,885 questions

51,811 answers

573 users

How to format date time into a string in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt = new DateTime(2016, 12, 23, 9, 25, 31);

            string sf = String.Format("{0:y yy yyy yyyy}", dt);
            Console.WriteLine(sf);

            Console.WriteLine(String.Format("{0:M MM MMM MMMM}", dt));
            Console.WriteLine(String.Format("{0:d dd ddd dddd}", dt));
            Console.WriteLine(String.Format("{0:y yy yyy yyyy}", dt));

            Console.WriteLine(String.Format("{0:h hh H HH}", dt)); 
            Console.WriteLine(String.Format("{0:m mm}", dt)); 
            Console.WriteLine(String.Format("{0:s ss}", dt));

            Console.WriteLine(String.Format("{0:MM/dd/yyy h:m:s}", dt));
        }
    }
}


/*
run:
     
16 16 2016 2016
12 12 Dec December
23 23 Fri Friday
16 16 2016 2016
9 09 9 09
25 25
31 31
12/23/2016 9:25:31
 
*/

 



answered Dec 23, 2016 by avibootz

Related questions

1 answer 88 views
1 answer 134 views
1 answer 133 views
2 answers 143 views
1 answer 41 views
...