Formatting dates.
-
Tuesday, February 26, 2013 9:11 PM
I am trying to figure out how to format a date column after it has been fetched from the database.
Here's my LINQ code.var query = (from x in myDataTable orderby x.PeriodStartDate select new {x.LeaseNo, x.ChargeCode, x.ContractID, x.PeriodStartDate});
Currently the periodStartDate column outputs date and time.
I want to truncate the time part and just display the date. The date has to be displayed as Day-Month-Year (23-Oct-2012).
How do I do that?
All Replies
-
Tuesday, February 26, 2013 9:45 PM
//Try
x.PeriodStartDate.ToString("dd-MMM-yyyy")
var query = (from x in myDataTable
orderby x.PeriodStartDate
select new
{x.LeaseNo, x.ChargeCode, x.ContractID, x.PeriodStartDate.ToString("dd-MMM-yyyy")});- Edited by Venkat786 Tuesday, February 26, 2013 9:47 PM
-
Tuesday, February 26, 2013 9:45 PM
I believe I have found the answer.
var query = (from x in myDataTable orderby x.PeriodStartDate select new {x1 = x.LeaseNo, x2 = x.ChargeCode, x3 = x.ContractID, x4 = x.PeriodStartDate.ToString()}) .ToList() .Select(o => o.x1 + " " + o.x2 + " " + " " + o.x3 + " "+ Convert.ToDateTime(o.x4).ToShortDateString());
- Edited by c-sharp-coder Tuesday, February 26, 2013 9:45 PM anonymity
-
Wednesday, February 27, 2013 4:15 PM
//Try
x.PeriodStartDate.ToString("dd-MMM-yyyy")
var query = (from x in myDataTable
orderby x.PeriodStartDate
select new
{x.LeaseNo, x.ChargeCode, x.ContractID, x.PeriodStartDate.ToString("dd-MMM-yyyy")});
If I use this code, it throws the following exception:
Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
So I made a slight change to what I posted earlier. Now my code looks like this:
var query = (from x in myDataTable orderby x.PeriodStartDate select new {x1 = x.LeaseNo, x2 = x.ChargeCode, x3 = x.ContractID, x4 = x.PeriodStartDate.ToString()}).ToList().Select(o => o.x1 + " " + o.x2 + " " + " " + o.x3 + " "+ Convert.ToDateTime(o.x4).ToString("yyyy-MMM-dd"));- Marked As Answer by c-sharp-coder Wednesday, February 27, 2013 4:15 PM

