我知道我可以做任何事情,还有一些更多关于日期的日期.但令人尴尬的是,我很难尝试做一些看起来很简单的事情:将两次之间的差异搞定.
例:
var now = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";
//expected result:
"00:39:30"
Run Code Online (Sandbox Code Playgroud)
我试过的:
var now = moment("04/09/2013 15:00:00");
var then = moment("04/09/2013 14:20:30");
console.log(moment(moment.duration(now.diff(then))).format("hh:mm:ss"))
//outputs 10:39:30
Run Code Online (Sandbox Code Playgroud)
我不明白那里的"10"是什么.我住在巴西,所以如果相关,我们是utc-0300.
的结果 moment.duration(now.diff(then))
是具有正确内部值的持续时间:
days: 0
hours: 0
milliseconds: 0
minutes: 39
months: 0
seconds: 30
years: 0
Run Code Online (Sandbox Code Playgroud)
所以,我想我的问题是:如何将momentjs持续时间转换为时间间隔?我当然可以使用
duration.get("hours") +":"+ duration.get("minutes") +:+ duration.get("seconds")
Run Code Online (Sandbox Code Playgroud)
但我觉得有一些更优雅,我完全失踪.
更新
近看,在上面的例子now
是:
Tue Apr 09 2013 15:00:00 GMT-0300 (E. South America Standard Time)…}
Run Code Online (Sandbox Code Playgroud)
并moment(moment.duration(now.diff(then)))
是:
Wed Dec 31 1969 22:39:30 GMT-0200 …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用moment.js确定两个日期之间是否有超过7天的时间。
码:
var start = moment(self.StartDate(), "DD/MM/YYYY");
var end = moment(self.EndDate(), "DD/MM/YYYY");
console.log(start);
console.log(end);
console.log(moment.duration(end.diff(start)).asDays());
if (moment.duration(end.diff(start)).asDays() > 7) {
alertify.alert("Error", "Only a maximum of 7 days can be investigated.");
return;
}
Run Code Online (Sandbox Code Playgroud)
如果两个日期都在同一月份内,则此方法有效。但是,如果日期跨越2个月,则duration
返回负值。
结果示例:
我正在尝试以天为单位计算两个日期之间的差异。但是,当两个日期在不同的月份时,上个月的日期将被丢弃。
例如,如果开始日期是 3 月 31 日,结束日期是 4 月 1 日,则差值为 0。
<ngb-datepicker #dp (select)="onDateSelection($event)" [displayMonths]="2" [dayTemplate]="t" outsideDays="hidden">
</ngb-datepicker>
<ng-template #t let-date let-focused="focused">
<span class="custom-day"
[class.focused]="focused"
[class.range]="isRange(date)"
[class.faded]="isHovered(date) || isInside(date)"
(mouseenter)="hoveredDate = date"
(mouseleave)="hoveredDate = null">
{{ date.day }}
</span>
</ng-template>
<p>Number of selected dates: {{DiffDate}}</p>
<pre>From: {{ fromDate | json }} </pre>
<pre>To: {{ toDate | json }} </pre>
Run Code Online (Sandbox Code Playgroud)
import {Component} from '@angular/core';
import {NgbDate, NgbCalendar} from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'ngbd-datepicker-range',
templateUrl: './datepicker-range.html',
styles: [`
.custom-day {
text-align: …
Run Code Online (Sandbox Code Playgroud) 嗨,你能告诉我如何使用 moment js 找到日期的差异吗?这是我的代码
https://jsfiddle.net/FLhpq/6082/
secondDate "2019-12-01" // formate YYYY-MM-DD;
var day = moment().format('YYYY-MM-DD');
// difference of secondDate - date
alert('day'+secondDate - day)
Run Code Online (Sandbox Code Playgroud)
我们可以计算两个日期之间的年差吗
预期产出
3
javascript ×3
momentjs ×3
difference ×2
angular ×1
date ×1
datetime ×1
jquery ×1
time ×1
typescript ×1