Skip to content

Instantly share code, notes, and snippets.

@GirlBossRush
Created July 4, 2014 07:22
Show Gist options
  • Save GirlBossRush/8ac2324e2dc139f9d929 to your computer and use it in GitHub Desktop.
Save GirlBossRush/8ac2324e2dc139f9d929 to your computer and use it in GitHub Desktop.
# Humanized timestamp component.
# Arguments:
# * datetime: ISO 8601 datetime String.
REFRESH_DELAY = 1000
React = require("react")
R = require("react-dom")
moment = require("moment/moment")
HumanTime = React.createClass
displayName: "humantime"
getInitialState: ->
{relativeTime: @humanize(@props.datetime)}
componentDidMount: ->
@interval = setInterval(@refresh, REFRESH_DELAY)
componentWillUnmount: ->
clearInterval @interval
humanize: (datetime) ->
moment(datetime).fromNow()
refresh: ->
@setState relativeTime: @humanize(@props.datetime)
render: ->
R.time {className: "human-time", dateTime: @props.datetime, title: @props.datetime},
@state.relativeTime
module.exports = HumanTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment