Question Dateformat in c# model.

lobsterTail

New member
Joined
Jun 3, 2023
Messages
4
Programming Experience
3-5
I have below class model.


C#:
public class Cart
{
public int CartDetailsId { get; set; }

        public int CartHeaderId { get; set; }

        public DateTime  StartingDate { get; set; }

        public int Count { get; set; }
}

I have List<Cart> subsequently produce a text file with above column headers. My question is- how can i format the StartingDate into the format i want using string.Format() string method in the model above so i don't have to loop into the list to do it after the fact?

something like

C#:
  public Date  StartingDate { get; set; } =string.Format()??

thanks.
 
You cannot do it ahead of time directly in your data structure (without being stringly-typed instead of strongly-typed). The internal date/time is stored as a 64-bit (or was it 128-bit?) number. It's only at output time that the string representation of the date/time is printed out. By default the string representation is dependent on your current UI settings, so for example if you set your dates to always have YYYY/MM/dd, and times to be HH:mm:ss, then that's what would be output.

If you want to influence how the date/time is displayed as a string:
  • use the ToString() method passing in a format string;
  • use the formatting options of WriteLine() or interpolated strings;
  • use a custom culture with your preferred formatting; OR
  • change your system settings with your preferred formatting.
 
You cannot do it ahead of time directly in your data structure (without being stringly-typed instead of strongly-typed). The internal date/time is stored as a 64-bit (or was it 128-bit?) number. It's only at output time that the string representation of the date/time is printed out. By default the string representation is dependent on your current UI settings, so for example if you set your dates to always have YYYY/MM/dd, and times to be HH:mm:ss, then that's what would be output.

If you want to influence how the date/time is displayed as a string:
  • use the ToString() method passing in a format string;
  • use the formatting options of WriteLine() or interpolated strings;
  • use a custom culture with your preferred formatting; OR
  • change your system settings with your preferred formatting.

ok thanks, that makes sense. i will convert to the format after the fact with the help of our friend
C#:
ToString()
 
You can be a little tricky and make it look like it was stored with that formatted date/time. For example the code below exposes a FormattedStartingDate which call ToString() on the actual value and returns that formatted value:
C#:
public class Cart
{
    public int CartDetailsId { get; set; }
    public int CartHeaderId { get; set; }
    public DateTime  StartingDate { get; set; }
    public int Count { get; set; }

    public string FormattedStartingDate => StartingDate.ToString("YYYY/MM/dd");
}
 
And then you might be able to add attributes so the DateTime version is ignored by the (e.g. csv) library you're using to write your file
 
Back
Top Bottom