File size: 818 Bytes
36a3951
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { time } from 'https://unpkg.com/ez-html-elements'

/** Create a date object from a standard string representation */
export default (dateString) => {
  // HtmlDate PRIVATE:

  function dateView () {
    const ms = Date.now() - Date.parse(dateString)
    if (ms < 1500) {
      return `${ms} ms`
    }
    const seconds = Math.round(ms / 1000)
    if (seconds < 90) {
      return `${seconds}s`
    }
    const minutes = Math.round(seconds / 60)
    if (minutes < 90) {
      return `${minutes}min`
    }
    const hours = Math.round(minutes / 60)
    if (hours < 90) {
      return `${hours}h`
    }
    const days = Math.round(hours / 24)
    return `${days}d`
  }

  // HtmlDate PUBLIC:
  return Object.freeze({
    /** Generate HTML text */
    html: () => time({ datetime: dateString }, dateView())
  })
}