Sha256: 0123e73626d607c5fc901171bc509cb11e172c2c9fc11458bd2a42009da728fb

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

import I18n from '../i18n'
import Env from '../env'

class Date
  constructor: (date) -> @date = date

  toString: (format = 'default') ->
    skope = I18n[Env.loco.getLocale()].date.formats
    switch format
      when 'default' then this.strftime skope.default
      when 'short' then this.strftime skope.short
      when 'long' then this.strftime skope.long
      else
        console.log 'Services.Date#toString: unknown format.'

  strftime: (str) ->
    skope = I18n[Env.loco.getLocale()]
    str = str.replace '%Y', (x) => @date.getFullYear()
    str = str.replace '%y', (x) => @date.getFullYear().toString()[-2..-1]
    str = str.replace '%m', (x) =>
      month = @date.getMonth() + 1
      if month >= 10 then month else "0#{month}"
    str = str.replace '%b', (x) => skope.date.abbr_month_names[@date.getMonth()]
    str = str.replace '%B', (x) => skope.date.month_names[@date.getMonth()]
    str = str.replace '%d', (x) => if @date.getDate() >= 10 then @date.getDate() else "0#{@date.getDate()}"
    str = str.replace '%H', (x) => if @date.getHours() >= 10 then @date.getHours() else "0#{@date.getHours()}"
    str = str.replace '%M', (x) => if @date.getMinutes() >= 10 then @date.getMinutes() else "0#{@date.getMinutes()}"
    str = str.replace '%S', (x) => if @date.getSeconds() >= 10 then @date.getSeconds() else "0#{@date.getSeconds()}"

export default Date

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
loco-rails-3.0.3 test/dummy/node_modules/loco-js/src/services/date.coffee
loco-rails-3.0.2 test/dummy/node_modules/loco-js/src/services/date.coffee
loco-rails-3.0.1 test/dummy/node_modules/loco-js/src/services/date.coffee
loco-rails-3.0.0 test/dummy/node_modules/loco-js/src/services/date.coffee