I have an ASP.NET MVC app. This app uses Razor as the view engine. I need to display a date/time to the user. My challenge is, the DateTime
property on the model is in UTC time. I want to display the time as the user's local time. Currently, I have the following:
<div>
@if (Model.LastModifiedOnUTC.HasValue) {
Html.Raw(Model.LastModifiedOnUTC.ToString());
}
else {
<span>This record has not been updated</span>
}
</div>
I want to display the DateTime
as something like "Wednesday, February 11, 2015 at 3:27 PM". However, I'm not sure how to do this. When I do the above, an empty string is printed. Which doesn't make any sense to me.
Thank you for any insights
One option is to use JavaScript.
in your View
<time>@Model.LastModifiedOnUTC.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)<time>
JS (my example with JQuery)
$("time").each(function (elem) {
var utctimeval = $(this).html();
var date = new Date(utctimeval);
$(this).html(date.toLocaleString());
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With