Sha256: 95b2c2ec893e410f0355a79ed95de55f434bcca6c96ace86d2f187ee81ef1863

Contents?: true

Size: 1.63 KB

Versions: 198

Compression:

Stored size: 1.63 KB

Contents

/* @flow */
/*eslint-disable flowtype/space-before-type-colon */

import moment from 'moment'
require('moment-strftime')
require('moment-timezone')

type DateTimeType = {
  value: String | Date,
  zone?: String,
}

const ABBR_DAYS = ['SU', 'M', 'T', 'W', 'TH', 'F', 'S']

export default class DateTime {
  constructor({ value, zone = 'America/New_York' }: DateTimeType) {
    this.value = this.convertToTimestampZone(value, zone)
  }

  convertToTimestampZone(value, zone) {
    return moment(value).tz(zone)
  }

  convertToTimezone() {
    return this.value.strftime('%Z')
  }

  toCustomFormat(format = '%-m/%-d') {
    return this.value.strftime(format)
  }

  toYear() {
    return this.value.strftime('%Y')
  }

  toMonth() {
    return this.value.strftime('%b')
  }

  toMonthNum() {
    return this.value.strftime('%-m')
  }

  toMonthFull() {
    return this.value.strftime('%B')
  }

  toDay() {
    return this.value.strftime('%e')
  }

  toDayAbbr() {
    return ABBR_DAYS[this.value.day()]
  }

  toWeekday() {
    return this.value.strftime('%a')
  }

  toHour() {
    return this.value.strftime('%l')
  }

  toMinute() {
    return this.value.strftime('%M')
  }

  toMeridian() {
    return this.value.strftime('%P')[0]
  }

  toIso() {
    return this.value.toISOString()
  }

  toTime() {
    const time = this.value.strftime('%I:%M')

    // strftime adds a leading 0 on single hour times. ie 08:31.
    // this removes that 0 to match the rails kit.
    return time.charAt() === '0' ? time.slice(1) : time
  }

  toTimezone() {
    return this.value.strftime('%Z')
  }

  toTimeWithMeridian() {
    return this.toTime() + this.toMeridian()
  }
}

Version data entries

198 entries across 198 versions & 1 rubygems

Version Path
playbook_ui-9.9.0.alpha.inline1 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.9.0 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.8.0 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.7.0.pre.alphawidth1 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.7.0.pre.alpha.a11y.btn app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.7.0 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.6.1 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.6.1.pre.deps1 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.6.0 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.5.0.alpha.rs.downgrade app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.5.0 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.4.0.alpha.sisense app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.4.0.alpha.user.kit.bug app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.4.0 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.3.1.pre.alpha2 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.4.0.pre.alpha2 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.4.0.pre.alpha1 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.3.1.pre.alpha1 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.3.0.alpha.inline3 app/pb_kits/playbook/pb_kit/dateTime.js
playbook_ui-9.3.0.pre.alpha.password.strength.2 app/pb_kits/playbook/pb_kit/dateTime.js