简体   繁体   中英

Convert Rails record dates to human readable date in Javascript

In javascript, I want to convert a standard date format given by Rails into something human readable.

Rails uses this for its records: 2014-06-17T19:48:42.072Z

Given that string in javascript, what is the easiest to get it into something where we can work with it? Ideally formatting it as June 17, 2014 ?

Assume you cannot transform the date with Rails before giving it to javascript.

37signals has a pretty cool rails engine/javascript plugin to display UTC times in a user's local time.

From their example:

> comment.created_at
"Wed, 27 Nov 2013 18:43:22 EST -0500"

<%= local_time(comment.created_at) %>

Renders:

<time data-format="%B %e, %Y %l:%M%P"
      data-local="time"
      datetime="2013-11-27T23:43:22Z">November 27, 2013 11:43pm</time>

When the DOM loads, the content is immediately replaced with a local, formatted time:

<time data-format="%B %e, %Y %l:%M%P"
      data-local="time"
      datetime="2013-11-27T23:43:22Z"
      title="November 27, 2013 6:43pm EDT"
      data-localized="true">November 27, 2013 6:43pm</time>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM