【问题标题】:Epoch/Unix timestamp (in ms) to Datetime in Razor ViewRazor 视图中的 Epoch/Unix 时间戳(以毫秒为单位)到日期时间
【发布时间】:2018-01-10 16:31:06
【问题描述】:

我有一个 .Net Core MVC 项目,我在其中使用 pagination 定义的 pr。微软文档。

在这种情况下,由于我的数据库时间戳在 Epoch 中,我需要将时间戳转换为日期时间对象,因为它用于我的 Razor 视图中的表。创建表如下:

@model PaginatedList<MSPFrontend.Tripmetadata>
<div class="table-responsive">
    <table class="table table-bordered table-condensed table-hover table-striped">
        <thead>
            <tr>
                <th>Trip Id</th>
                <th>Start Time</th>
                <th>End Time</th>
                <th>Duration</th>
                <th>Avg Speed</th>
                <th>Distance</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr class="trip" data-id="@item.Tripid" data-url="@Url.Action("TripMapping", "Trip")">
                    <td>@item.Tripid</td>
                    <td>@item.Starttimestamp</td>
                    <td>@item.Endtimestamp</td>
                    <td>@item.Duration</td>
                    <td>@item.AvgSpeed knots</td>
                    <td>@item.Distance km</td>
                </tr>
            }
        </tbody>
    </table>
</div>

我的模型如下所示:

public partial class Tripmetadata
{
    public Tripmetadata()
    {
    }

    [Key]
    public int Tripid { get; set; }

    [Display(Name = "Start Timestamp")]
    public long? Starttimestamp { get; set; }

    [Display(Name = "End Timestamp")]
    public long? Endtimestamp { get; set; }

    public long? Duration { get; set; }

    [Display(Name = "Average Speed")]
    public decimal? AvgSpeed { get; set; }

    public decimal? Distance { get; set; }
}

我以前使用 ViewModel 来处理这个问题,但作为 pr。 Microsoft Pagination 文档分页需要我使用我的模型,因为它在我滚动分页时请求数据。

我认为可以这样做:

<td>@new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(@Convert.ToDouble(@item.Starttimestamp))</td>

但我在尝试时得到以下信息:

jquery.js:9566 GET http://localhost:1048/Trip/TripTable 500 (Internal Server Error)

【问题讨论】:

    标签: c# datetime razor asp.net-core-mvc epoch


    【解决方案1】:

    我设法找到了实际上非常简单的解决方案。

    在我的@foreach(var item in Model) 中,我可以进行转换,尽管这可能不是最好的方法。

    我最终有以下看法:

    <div class="table-responsive">
        <table class="table table-bordered table-condensed table-hover table-striped">
            <thead>
                <tr>
                    @{ 
                        var headerItem = Model[1];
                    }
                    <th>@Html.DisplayNameFor(modelItem => headerItem.Tripid)</th>
                    <th>@Html.DisplayNameFor(modelItem => headerItem.Starttimestamp)</th>
                    <th>@Html.DisplayNameFor(modelItem => headerItem.Endtimestamp)</th>
                    <th>@Html.DisplayNameFor(modelItem => headerItem.Duration)</th>
                    <th>@Html.DisplayNameFor(modelItem => headerItem.AvgSpeed)</th>
                    <th>@Html.DisplayNameFor(modelItem => headerItem.Distance)</th>
                </tr>
            </thead>
            <tbody>
                @foreach(var item in Model)
                {
                    var startTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(Convert.ToDouble(@item.Starttimestamp));
                    var endTime = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(Convert.ToDouble(@item.Endtimestamp));
                    double diff = Convert.ToDouble(item.Endtimestamp) - Convert.ToDouble(item.Starttimestamp);
                    var duration = TimeSpan.FromMilliseconds(diff);
                    var avgSpeed = Math.Truncate(100 * Convert.ToDecimal(item.AvgSpeed)) / 100;
                    var distance = Math.Truncate(100 * Convert.ToDecimal(item.Distance)) / 100;
                    <tr class="trip" data-id="@item.Tripid" data-url="@Url.Action("TripMapping", "Trip")">
                        <td>@item.Tripid</td>
                        <td>@startTime</td>
                        <td>@endTime</td>
                        <td>@duration</td>
                        <td>@avgSpeed knots</td>
                        <td>@distance km</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    

    表格标题下的部分允许我在模型中定义标题。如下:

    public partial class Tripmetadata
    {
        public Tripmetadata()
        {
        }
    
        [Key]
        [Display(Name = "Trip ID")]
        public int Tripid { get; set; }
    
        [Display(Name = "Start Timestamp")]
        public long? Starttimestamp { get; set; }
    
        [Display(Name = "End Timestamp")]
        public long? Endtimestamp { get; set; }
    
        [Display(Name = "Duration")]
        public long? Duration { get; set; }
    
        [Display(Name = "Average Speed")]
        public decimal? AvgSpeed { get; set; }
    
        [Display(Name = "Distance")]
        public decimal? Distance { get; set; }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 2016-04-25
      相关资源
      最近更新 更多