Skip to content

5.1: Create Time Ago Utility #94

@justin808

Description

@justin808

Overview

Create utility for displaying relative time.

Parent Epic: #70
Priority: P1

Prerequisites

Next Issues (after this is complete)

  • Can be used by Story and Comment components

Acceptance Criteria

  • app/javascript/lib/timeAgo.ts created
  • Converts Unix timestamp to relative time string
  • Handles: "just now", "X minutes ago", "X hours ago", "X days ago"
  • Handles edge cases (future dates, very old dates)
  • Unit tests with Jest/Vitest
  • TypeScript types exported

Implementation Notes

export function timeAgo(timestamp: number): string {
  const seconds = Math.floor(Date.now() / 1000 - timestamp);
  
  if (seconds < 60) return 'just now';
  if (seconds < 3600) return `${Math.floor(seconds / 60)} minutes ago`;
  if (seconds < 86400) return `${Math.floor(seconds / 3600)} hours ago`;
  return `${Math.floor(seconds / 86400)} days ago`;
}

Note: This could also be done server-side in Ruby (in the presenters). Decide based on where it's most useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    HN-RSCHacker News RSC DemoRSCReact Server Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions