locked
How to format a date as a constant. RRS feed

  • Question

  • User-501297529 posted

    I have a Constants class file in my solution that is used for roles and messages and I need to create one for date formatting. Currently on several views, I have the date formatted as 

          <div>
                <div class="card-body">
                    <h4 class="card-title">
                        <a class="forum-orange"
                           style="text-decoration:underline"
                           asp-action="Packet"
                           asp-controller="Home"
                           asp-route-packetId="@Model.PacketId">
                            Meeting: 
                            @Model.Title
                        </a>
                    </h4>
                    <div class="card-text">
                        Meeting date: <b>
                            @Model.MeetingDateTime.ToString("dddd, MMMM dd yyyy h:mm tt")
                        </b>
                    </div>
    
                    <div class="card-text">
                        Meeting time:
                        <b> @Model.MeetingDateTime.ToString("hh:mm tt") </b>
                    </div>
                    <p class="card-text">
                        <h6 class="forum-orange">
                            Voting has expired on<br/>
                            @Model.CODateTime.ToString("dddd, MMMM dd yyyy h:mm tt")
                        </h6>
                    </p>
                </div>
            </div>

    I added this to my constants class:

     public static class FormatDate
            {
    
               public const string DEFAULT_DATE_FORMAT = "dddd, MMMM dd yyyy h:mm tt";
    
            }

    And this updated the view:

      <div>
                <div class="card-body">
                    <h4 class="card-title">
                        <a class="forum-orange"
                           style="text-decoration:underline"
                           asp-action="Packet"
                           asp-controller="Home"
                           asp-route-packetId="@Model.PacketId">
                            Meeting: 
                            @Model.Title
                        </a>
                    </h4>
                    <div class="card-text">
                        Meeting date: <b>
                            @Model.MeetingDateTime.ToString(Constants.FormatDate.DEFAULT_DATE_FORMAT)
                        </b>
                    </div>
    
                    <div class="card-text">
                        Meeting time:
                        <b> @Model.MeetingDateTime.ToString("hh:mm tt") </b>
                    </div>
                    <p class="card-text">
                        <h6 class="forum-orange">
                            Voting has expired on<br/>
                            @Model.CODateTime.ToString(Constants.FormatDate.DEFAULT_DATE_FORMAT)
                        </h6>
                    </p>
                </div>
            </div>

    Would this be correct?

    Wednesday, May 20, 2020 8:14 PM

Answers

  • User-501297529 posted

    I think the best way may be an extension method, maybe? I'm not sure how to do that using the format that I'm using for the datetime. How would I do that?

    This is what I came up with

    Extension class

     public static class LongDateExtension
        {
            public static string ToLongDateString(this DateTime dateTime)
            {
                return dateTime.ToString("dddd, MMMM dd yyyy h:mm tt");
            }
        }

    View

    <div>
                <div class="card-body">
                    <h4 class="card-title">
                        <a class="forum-orange"
                           style="text-decoration:underline"
                           asp-action="Packet"
                           asp-controller="Home"
                           asp-route-packetId="@Model.PacketId">
                            Meeting: 
                            @Model.Title
                        </a>
                    </h4>
                    <div class="card-text">
                        Meeting date: <b>
                            @Model.MeetingDateTime.ToLongDateString()
                        </b>
                    </div>
    
                    <div class="card-text">
                        Meeting time:
                        <b> @Model.MeetingDateTime.ToString("hh:mm tt") </b>
                    </div>
                    <p class="card-text">
                        <h6 class="forum-orange">
                            Voting has expired on<br/>
                            @Model.CODateTime.ToLongDateString()
                        </h6>
                    </p>
                </div>
            </div>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 3:41 PM

All replies

  • User303363814 posted

    Would this be correct?
    What result do you see?

    If the result is what you want then it is correct.  If it is not what you want then it is not correct.

    Wednesday, May 20, 2020 11:00 PM
  • User-501297529 posted

    bootzilla

    Would this be correct?

    What result do you see?

    If the result is what you want then it is correct.  If it is not what you want then it is not correct.

    I got the same result that I did before changing the code. I just want to make sure my naming conventions and code look ok even though it works, if you know what I mean?

    Wednesday, May 20, 2020 11:04 PM
  • User303363814 posted

    It looks like you are trying to re-invent the localization wheel.  I'm not a razor person but https://www.mikesdotnetting.com/article/346/using-resource-files-in-razor-pages-localisation seems like a good introduction to the topic.  For good measure the author is a moderator on these forums https://forums.asp.net/members/Mikesdotnetting.aspx  so help is not too far away.

    Wednesday, May 20, 2020 11:48 PM
  • User-501297529 posted

    It looks like you are trying to re-invent the localization wheel.  I'm not a razor person but https://www.mikesdotnetting.com/article/346/using-resource-files-in-razor-pages-localisation seems like a good introduction to the topic.  For good measure the author is a moderator on these forums https://forums.asp.net/members/Mikesdotnetting.aspx  so help is not too far away.

    Not sure I want to do all of this. I know I can create a resource file but since I have a constants class already set up wouldn't it be easier just to do it the way I did it?

    Thursday, May 21, 2020 1:30 AM
  • User711641945 posted

    Hi bootzilla,

    I tried many other ways,for you do not serialize the data,just display it.I think what you did is the much easier way to use ToString("<Use formatting here>").

    For if you need to serialize the Datetime,you could custom JsonConverter,refer to:

    https://stackoverflow.com/a/58103218/11398810

    Best Regards,

    Rena

    Thursday, May 21, 2020 8:00 AM
  • User753101303 posted

    Hi,

    An alternate option could be to set the culture you want to target, use data annotations to tell if you want a "short" or "long" format and literal tag helper to use this information. It would allow to support other languages as well.

    I try to always stay away from a local explicit date format (even as a constant) in favor of using the current culture which I set according to my needs.

    Thursday, May 21, 2020 1:12 PM
  • User-501297529 posted

    I think the best way may be an extension method, maybe? I'm not sure how to do that using the format that I'm using for the datetime. How would I do that?

    This is what I came up with

    Extension class

     public static class LongDateExtension
        {
            public static string ToLongDateString(this DateTime dateTime)
            {
                return dateTime.ToString("dddd, MMMM dd yyyy h:mm tt");
            }
        }

    View

    <div>
                <div class="card-body">
                    <h4 class="card-title">
                        <a class="forum-orange"
                           style="text-decoration:underline"
                           asp-action="Packet"
                           asp-controller="Home"
                           asp-route-packetId="@Model.PacketId">
                            Meeting: 
                            @Model.Title
                        </a>
                    </h4>
                    <div class="card-text">
                        Meeting date: <b>
                            @Model.MeetingDateTime.ToLongDateString()
                        </b>
                    </div>
    
                    <div class="card-text">
                        Meeting time:
                        <b> @Model.MeetingDateTime.ToString("hh:mm tt") </b>
                    </div>
                    <p class="card-text">
                        <h6 class="forum-orange">
                            Voting has expired on<br/>
                            @Model.CODateTime.ToLongDateString()
                        </h6>
                    </p>
                </div>
            </div>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 21, 2020 3:41 PM
  • User753101303 posted

    Hi,

    I would start by using the correct culture and then 

    @Model.MeetingDateTime.ToString("f")

    See https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#the-full-date-short-time-f-format-specifier

     which should render what you want for your culture (but could work as well for other cultures)

    Thursday, May 21, 2020 8:06 PM