0

I'm trying to remove the time portion of a DateTimeOffset field in an Excel export and I just cannot get it to work.

Here's the field I'm using to display:

[DataType(DataType.DateTime)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime ProjectStartDate { get; set; }

Here's the field I'm mapping from:

public DateTimeOffset ProjectStartDate { get; set; }

and here's where I'm doing the mapping:

ProjectStartDate = p.ProjectStartDate,

I'm using EPPlus to generate the export as follows:

ExcelWorksheet wsExpenses = pck.Workbook.Worksheets.Add("Expenses");

wsExpenses.Cells["A1"].LoadFromCollection(expenses, true);
wsExpenses.Column(5).Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
jeroenh
  • 26,362
  • 10
  • 73
  • 104
TrevorGoodchild
  • 978
  • 2
  • 23
  • 49
  • `ProjectStartDate = p.ProjectStartDate.Date`? – jeroenh Aug 13 '18 at 18:54
  • Tried that earlier, got: "The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.' – TrevorGoodchild Aug 13 '18 at 18:56
  • That was not clear in your question. Could you please read this for guidance on writing good questions on Stack Overflow: https://stackoverflow.com/help/how-to-ask – jeroenh Aug 13 '18 at 19:02
  • In the interest of keeping it short, I omitted about 10 different things that I tried already. – TrevorGoodchild Aug 13 '18 at 19:09
  • It's excellent that you try to keep it short, however to maximize your chances of getting help, it's important to provide all necessary context to reproduce the issue at hand... See also [How to create a Minimal, Complete, Verifiable example](https://stackoverflow.com/help/mcve) – jeroenh Aug 13 '18 at 19:18
  • 1
    Terrific advice, thank you! – TrevorGoodchild Aug 13 '18 at 19:19
  • All this being said, I suspect you may need an intermediate step in your mapping, where you first get the property as a DateTimeOffset, materialize the result in memory and only then strip the date part – jeroenh Aug 13 '18 at 19:23

1 Answers1

0

Figured it out, had to pull it apart in the mapping:

ProjectStartDate = DbFunctions.CreateDateTime(p.ProjectStartDate.Year, p.ProjectStartDate.Month, p.ProjectStartDate.Day,
                            p.ProjectStartDate.Hour, p.ProjectStartDate.Minute, p.ProjectStartDate.Second),
TrevorGoodchild
  • 978
  • 2
  • 23
  • 49