ServiceStack Razor当地时间

时间:2018-01-25 03:52:25

标签: servicestack

ServiceStack Razor页面显示时间是UTC,如何显示本地时间?

Backstage中的代码:

ServiceModel.Types

public class Customer
{
    public int Id { get; set; }

    public DateTime CreateDate { get; set; }
}

Service.Model

[Route("/customers/{id}", "GET", Matches = "**/{int}")]
public class GetCustomer
{
    public int Id { get; set; }
}

public class GetCustomerResponse
{
    public int Id { get; set; }

    public DateTime CreateDate { get; set; }
}

serviceInterface等

public class CustomerService : Service
{
    public AppConfig Config { get; set; }

    [DefaultView("Customer")]
    public object Get(GetCustomer request)
    {
        var customer = Db.SingleById<Customer>(request.Id);
        return new GetCustomerResponse().PopulateWith(customer);
    }
}

页面中的代码:

<span>@Model.CreateDate</span>

DB中的CreateDate存储是UTC时间,如何在Http Custom挂钩中将其转换为LocalTime。然后在页面中显示LocalTime。

1 个答案:

答案 0 :(得分:1)

ServiceStack.Razor中显示时间与普通C#相同,例如:

<pre>
Local Time:

@DateTime.Now

UTC Time:

@DateTime.UtcNow

Formatted Local Time:

@DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
</pre>

这篇文章在EST的哪个时间呈现:

Local Time:

1/24/2018 11:12:06 PM

UTC Time:

1/25/2018 4:12:06 AM

Formatted Local Time:

2018/01/24 23:12:06

要将UTC时间转换为LocalTime,您可以使用ToLocalTime(),例如:

@Model.CreateDate.ToLocalTime()