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

51,825 answers

573 users

How to format datetime in different formats with C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        DateTime dt = DateTime.Now;  
  
        Console.WriteLine(dt.ToString("1. MM/dd/yyyy"));  
        Console.WriteLine(dt.ToString("2. MMMM dd"));  
        Console.WriteLine(dt.ToString("3. yyyy MMMM"));  
        Console.WriteLine(dt.ToString("4. dddd, dd MMMM yyyy"));  
        Console.WriteLine(dt.ToString("5. dddd, dd MMMM yyyy HH:mm:ss"));  
        Console.WriteLine(dt.ToString("6. MM/dd/yyyy H:mm"));  
        Console.WriteLine(dt.ToString("7. MM/dd/yyyy HH:mm"));  
        Console.WriteLine(dt.ToString("8. MM/dd/yyyy hh:mm tt"));  
        Console.WriteLine(dt.ToString("9. MM/dd/yyyy h:mm tt"));  
        Console.WriteLine(dt.ToString("10. MM/dd/yyyy HH:mm:ss"));  
        Console.WriteLine(dt.ToString("11. yyyy-MM-dd HH:mm:ss.fffffffK"));  
        Console.WriteLine(dt.ToString("12. ddd dd MMM yyy HH:mm:ss GMT"));  
        Console.WriteLine(dt.ToString("13. yyyy-MM-dd HH:mm:ss"));  
        Console.WriteLine(dt.ToString("14. HH:mm"));  
        Console.WriteLine(dt.ToString("15. HH:mm:ss"));  
        Console.WriteLine(dt.ToString("16. H:mm"));  
        Console.WriteLine(dt.ToString("17. hh:mm tt"));  
        Console.WriteLine(dt.ToString("18. h:mm tt"));  
    }
}




/*
run:

1. 11/17/2021
2. November 17
3. 2021 November
4. Wednesday, 17 November 2021
5. Wednesday, 17 November 2021 06:55:16
6. 11/17/2021 6:55
7. 11/17/2021 06:55
8. 11/17/2021 06:55 AM
9. 11/17/2021 6:55 AM
10. 11/17/2021 06:55:16
11. 2021-11-17 06:55:16.0251460+00:00
12. Wed 17 Nov 2021 06:55:16 G11T
13. 2021-11-17 06:55:16
14. 06:55
15. 06:55:16
16. 6:55
17. 06:55 AM
18. 6:55 AM

*/

 



answered Nov 17, 2021 by avibootz

Related questions

1 answer 143 views
1 answer 107 views
1 answer 166 views
1 answer 160 views
1 answer 172 views
...