最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Same date format for 'input type="date"' and display in table - Stack Overflow

programmeradmin1浏览0评论

I am having trouble showing the date in the correct format. On one site I enter the date with the function:

<td><input type="date" class="form-control"/> </td>

As far as I understand, type date uses the browser date format.

On a table overview site I want to show the before entered date in the same format (I would go with the browser format as well). I tried so far:

@using System.Globalization
<td>@a.EnabledUntil.Value.ToString("d", CultureInfo.CurrentUICulture)</td>

or:

<td>@a.EnabledUntil.Value.ToString("d", CultureInfo.CurrentCulture)</td>

My problem is, on the input the format is dd/MM/yyyy, but on the overview it is MM/dd/yyyy.

On the model it has the type DateTime and is stored in a SQLite DB.

I don't want to hard code the date format, but how can I align both in the same format?

Thanks

Stephan

Edit: I used finally the ChatGpt solution with javascript:

<td class="date-cell" data-date="@a.EnabledUntil?.ToString("yyyy-MM-dd")"></td>

and JavaScript:

<script>
document.addEventListener("DOMContentLoaded", function () {
    document.querySelectorAll(".date-cell").forEach(td => {
        const rawDate = td.getAttribute("data-date");
        if (rawDate) {
            td.textContent = new Date(rawDate).toLocaleDateString(); // Uses the browser’s locale format
        }
    });
});
</script>

I am just not sure if this is a good way, but it works when I switch browsers language

发布评论

评论列表(0)

  1. 暂无评论