• 0

[LINQ C#] Show only Date from DateTime type


Question

1 answer to this question

Recommended Posts

  • 0

In LINQ C# how can I display only the Date from a type DateTime?

For example:

namespace WindowsFormsApplication1 {

    using System;
    using System.Linq;
    using System.Windows.Forms;
    using System.Collections.Generic;

    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
        }

        public class Customer {

            public Guid ID { get; set; }
            public string Name { get; set; }
            public DateTime Created { get; set; }

            public Customer() {
            }
        }

        private void button1_Click(object sender, EventArgs e) {

            List<Customer> customers = new List<Customer>();

            customers.Add(new Customer() { ID = Guid.NewGuid(), Name = "AAA", Created = DateTime.Now }); 
            customers.Add(new Customer() { ID = Guid.NewGuid(), Name = "BBB", Created = DateTime.Now }); 
            customers.Add(new Customer() { ID = Guid.NewGuid(), Name = "CCC", Created = DateTime.Now });

            var o = from customer in customers 
                    select new {
                        ID = customer.ID,
                        Name = customer.Name,
                        CreatedFormatted = customer.Created.ToString("dd.MM.yyyy")  // Add your date formatting here
                    };

            foreach (var customer in o) {
                Console.WriteLine(customer.ID + ", " + customer.Name + ", " + customer.CreatedFormatted);
            }
        }
    }
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.