Skip to content

Instantly share code, notes, and snippets.

@pipedreambomb
Last active September 1, 2016 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pipedreambomb/d65ec95281e598f4012bf87ba9f4ed1d to your computer and use it in GitHub Desktop.
Save pipedreambomb/d65ec95281e598f4012bf87ba9f4ed1d to your computer and use it in GitHub Desktop.
<span id='date-me'></span>
<script type='text/JavaScript'>document.getElementById('date-me').innerHTML = new Date(Date.parse('Wed, 09 Aug 1995 00:00:00 GMT')).toLocaleString()</script>
@pipedreambomb
Copy link
Author

My idea is to write an HTML helper in ASP.NET MVC that can convert a datetime to a local date, and you don't need to do anything but supply the date.

I'll have to generate the IDs uniquely, probably by field name with some prefix or suffix.

The date information will come from the .NET DateTime object, and will need filling in slightly laboriously (as in .GetYear() .GetMonth() etc) as JavaScript doesn't seem to have a native way of parsing UTC dates, and I'd rather not use a library like moment.js so that the helper always works and has no dependencies.

@pipedreambomb
Copy link
Author

Update: I found Date.parse() does work with UTC as long as the string is formatted in one of a few supported ways.

I think outputting a DateTime as RoundTrip ought to do it.
https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Roundtrip
Might need to set the DateTimeKind first:
https://msdn.microsoft.com/en-us/library/system.datetime.specifykind(v=vs.110).aspx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment