I am using Razor as a view engine in ASP.NET MVC 5. I have dates coming back from a back-end service that are in UTC. I want to format those using JavaScript (e. g. moment.js
) on the client. I don't want to do it on the server, because I don't want to have to worry about what time zone the client is in on the server side (after all, the server doesn't really care).
I think from Locale Date formatting with MVC, AJAX and Moment.js that there's some way to do this server-side if I have to, although there's not enough information for me to get it worked out, and that's not what I want to do anyway.
Something like ASP.NET MVC ViewModel mapping with custom formatting or Where is the best place to format Model properties in ASP.NET MVC(3)? or How to format date in asp.net mvc 5 doesn't work because that's server side (or does it in some way I don't understand?)
What's the "best" / "right" way to do this cleanly and reliably?
EDIT: To be clear, I know all about the moment.js
side. I wouldn't have named it if I didn't. What I'm asking about is the ASP.NET MVC view integration - how do I get that done the "right" way?
EDIT 2: I know how to do the script. What I don't know is if I have that output, from a script variable, how do I get the contents of that variable into the information displayed to the user in the view? I don't want to have to do something like edit the HTML element contents or something, if I can help it; that's not clean at all. Perhaps I should make this much simpler to make this very very clear:
Imagine a view in Razor. That view says:
@foreach (var item in Model) {
<!-- something to do javascript manipulation of item -->
<p>The result of manipulating @item.startDate is <!-- WHAT DO I DO HERE? --></p>
}
The comments are what I don't know how to do right now cleanly.