片刻js - 显示距离约会的日期和时间

时间:2017-07-03 15:58:23

标签: javascript momentjs

我正在开发一个需要在日期对象上进行此类文本响应的项目​​。

“1天7小时之后” ---它需要这样 - 不是“31小时之遥”或“1天之后” - 我也在使用片刻js - 因为我正在进行英语和德语之间的语言切换 - 所以我已经进入了moment.js语言区域

==> box: Adding box 'laravel/homestead' (v2.1.0) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/2.1.0/providers/virtualbox.box
==> box: Box download is resuming from prior download progress
    box: Progress: 100% (Rate: 444k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'laravel/homestead' (v2.1.0) for 'virtualbox'!

我正在使用时刻js - 目前我已经创建了一个假日期对象

    λ vagrant up
Bringing machine 'homestead-7' up with 'virtualbox' provider...
==> homestead-7: Importing base box 'laravel/homestead'...
C:/HashiCorp/Vagrant/embedded/gems/gems/childprocess-0.6.3/lib/childprocess/windows/process_builder.rb:43:in `

join': incompatible character encodings: GBK and UTF-8 (Encoding::CompatibilityError)
        from C:/HashiCorp/Vagrant/embedded/gems/gems/childprocess-0.6.3/lib/childprocess/windows/process_builder.rb:43:in `create_command_pointer'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/childprocess-0.6.3/lib/childprocess/windows/process_builder.rb:27:in `start'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/childprocess-0.6.3/lib/childprocess/windows/process.rb:70:in `launch_process'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/childprocess-0.6.3/lib/childprocess/abstract_process.rb:82:in `start'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/subprocess.rb:141:in `block in execute'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/safe_chdir.rb:26:in `block (2 levels) in safe_chdir'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/safe_chdir.rb:25:in `chdir'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/safe_chdir.rb:25:in `block in safe_chdir'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/safe_chdir.rb:24:in `synchronize'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/safe_chdir.rb:24:in `safe_chdir'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/subprocess.rb:140:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/subprocess.rb:22:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/driver/base.rb:440:in `block in raw'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/busy.rb:19:in `busy'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/driver/base.rb:439:in `raw'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/driver/base.rb:377:in `block in execute'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/retryable.rb:17:in `retryable'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/driver/base.rb:372:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/driver/version_5_0.rb:318:in `import'
        from C:/HashiCorp/Vagrant/embedded/mingw64/lib/ruby/2.3.0/forwardable.rb:204:in `import'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/action/import.rb:53:in `import'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/action/import.rb:13:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/action/prepare_clone_snapshot.rb:17:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/builtin/prepare_clone.rb:15:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/action/customize.rb:40:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/action/check_accessible.rb:18:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:95:in `block in finalize_action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/builder.rb:116:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/runner.rb:66:in `block in run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/busy.rb:19:in `busy'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/runner.rb:66:in `run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/builtin/call.rb:53:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/builtin/config_validate.rb:25:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:95:in `block in finalize_action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/builtin/handle_box.rb:56:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:95:in `block in finalize_action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/builder.rb:116:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/runner.rb:66:in `block in run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/busy.rb:19:in `busy'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/runner.rb:66:in `run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/builtin/call.rb:53:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/plugins/providers/virtualbox/action/check_virtualbox.rb:17:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/warden.rb:34:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/builder.rb:116:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/runner.rb:66:in `block in run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/util/busy.rb:19:in `busy'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/action/runner.rb:66:in `run'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/machine.rb:227:in `action_raw'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/machine.rb:202:in `block in action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/environment.rb:567:in `lock'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/machine.rb:188:in `call'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/machine.rb:188:in `action'
        from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.6/lib/vagrant/batch_action.rb:82:in `block (2 levels) in run'

当我尝试渲染时刻js

moment.locale('de')

它只是说“一天”

如何修改力矩函数以处理1天7小时 - 并且可能重新判断句子?

---代码段尝试

  var futureDate = new Date()
  futureDate.setDate(futureDate.getDate() + 1)// add a day
  futureDate.setHours(7)// add 7 hours
moment(futureDate).endOf('day').fromNow()

代码测试2使用差异

moment.locale('de') // switch between en and de -- english and german

var futureDate = new Date()
futureDate.setDate(futureDate.getDate() + 1)// add a day
futureDate.setHours(7)// add 4 hours

// Results in hours
console.log(moment(futureDate).endOf('day').fromNow()); 
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

3 个答案:

答案 0 :(得分:2)

您可以使用relativeTimeThresholdrelativeTimefromNow()的键)来自定义时刻显示相对时间的方式(例如var momEn = moment().add({d:1, h:7}); var momDe = moment().locale('de').add({d:1, h:7}); console.log(momEn.fromNow()); // in a day console.log(momDe.fromNow()); // in einem Tag // Change relativeTimeThreshold moment.relativeTimeThreshold('s', 60*60*24*30*12); // Update relative time moment.updateLocale('en', { relativeTime : { s: function (number, withoutSuffix, key, isFuture){ return moment.duration(number, 's').format('d [day] h [hour]'); }, } }); moment.updateLocale('de', { relativeTime : { s: function (number, withoutSuffix, key, isFuture){ return moment.duration(number, 's').format('d [Tag] h [Uhr]'); }, } }); console.log(momEn.fromNow()); // in 1 day 7 hour console.log(momDe.fromNow()); // in 1 Tag 7 Uhr输出)。

在您的情况下,您可以:

这是一个实时样本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
@tailrec
 def shift(times:Int, data:Array[Int]):Array[Int] = times match {
        case t:Int if(t <= 0) => data;
        case t:Int if(t <= data.length) => shift(0, (data++data.take(times)).drop(times)) 
        case _ => shift(times % data.length, data);
}

不幸的是,您必须手动更新需要支持的每个区域设置。

答案 1 :(得分:1)

您可以使用moment-duration-formatmoment.diff执行此操作:

let futureDate = new Date ()
futureDate.setDate (futureDate.getDate () + 1)// add a day
futureDate.setHours (7)
let start = moment ()
let end = moment (futureDate)

// 1st solution: with moment-duration-format

console.log (moment.duration (end.diff (start)).format ('d [days] hh [hours]', { trim: false }))

// 2nd solution: diff without moment-duration-format

let hoursDuration = end.diff (start, 'hours')
console.log (Math.floor (hoursDuration / 24) + ' days ' + (hoursDuration % 24) + ' hours')
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>

第一个解决方案需要额外的moment-duration-format模块,而第二个解决方案只使用moment.js执行所有操作。另外,在要求之前不要忘记npm install moment-duration-format

答案 2 :(得分:0)

修改:由于您提到您希望坚持使用Moment.js,因此可以使用moment#diff

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1

发现自:https://stackoverflow.com/a/42187373/2803743

我会使用countdown.js

var futureDate = new Date()
futureDate.setDate(futureDate.getDate() + 1)// add a day
futureDate.setHours(7)// add 7 hours
var timePassed = countdown(Date.now().toString(), futureDate, countdown.DAYS|countdown.HOURS);

console.log(timePassed);

timePassed是一个可爱的对象;在这个例子中:

  days: 0
  end: Tue Jul 04 2017 07:17:41 GMT+0300 (EEST)
  hours: 12
  start: Mon Jul 03 2017 19:17:41 GMT+0300 (EEST)
  units: 18
  value: 43200000

然后你可以连接到你想要的字符串。

它没有真正记录好,但lib也有一个CDN https://cdnjs.com/libraries/countdown