我已经执行了这些简单的JS代码行
moment.duration(moment(new Date(2013,1,1)).diff(moment(new Date(2012,1,1)))).asYears()
moment.duration(moment(new Date(2012,1,1)).diff(moment(new Date(2011,1,1)))).asYears()
moment.duration(moment(new Date(2011,1,1)).diff(moment(new Date(2010,1,1)))).asYears()
moment.duration(moment(new Date(2010,1,1)).diff(moment(new Date(2009,1,1)))).asYears()
Run Code Online (Sandbox Code Playgroud)
输出
1.0020739645577939
0.9993360575508053
0.9993360575508053
0.9993360575508053
Run Code Online (Sandbox Code Playgroud)
当然,一定有问题吗?
或许这种异常与闰年有关?2012年是闰年
所以我尝试了2016年的下一个闰年
moment.duration(moment(new Date(2016,1,1)).diff(moment(new Date(2015,1,1)))).asYears()
Run Code Online (Sandbox Code Playgroud)
其输出与之前的非闰年相同,所以可能与之无关
0.9993360575508053
Run Code Online (Sandbox Code Playgroud)
任何人都知道发生了什么事吗?
首先,我希望这不是重复.我已经阅读了很多类似的问题,但找不到这个问题.
我有一个javascript datepicker,它在内部使用javascript日期,这有一个意想不到的副作用.当我选择2016年4月30日时,它会在该日期返回午夜(00:00)的日期对象,但如果您在不同的时区并且在内部使用UTC,则会返回一个补偿UTC偏移的对象.所以,因为它是BST,所以我回来的日期是4月30日00:00 GTM + 1:00,当你把它变成ISO字符串时实际上是4月29日23:00.
我想将正确的日期(4月30日,没有时间)发送回服务器,但是当前执行此操作时我得到的日期字符串:
var newDate = moment(newValue).toISOString();
console.log(newDate);
Run Code Online (Sandbox Code Playgroud)
这是:
2016-05-19T23:00:00.000Z
Run Code Online (Sandbox Code Playgroud)
所以,我对此的理解很弱,但我认为,如果并且在19日23:00作为UTC日期在内部存储,那么日期选择器最初使用我的本地时间偏移需要一个小时,所以它已经是一个UTC日期,转换为UTC无济于事.
我需要做的是使用moment.js来补偿utcOffset并将日期移动到我在UTC中选择的日期的午夜.
我正在使用带有nodejs的momentjs.com,我试图获得多个时区的unix时间戳,但遗憾的是输出不正确.
码:
var moment = require('moment-timezone');
var berlin = moment.tz('Europe/Berlin').unix();
var angeles = moment.tz('America/Los_Angeles').unix();
var london = moment.tz('Europe/London').unix();
console.log(berlin);
console.log(angeles);
console.log(london);
Run Code Online (Sandbox Code Playgroud)
输出:
1472241731
1472241731
1472241731
Run Code Online (Sandbox Code Playgroud) 我试图将整个月的最后一天作为整数.因此,应该返回12月31日.
moment().endOf('month').day()
Run Code Online (Sandbox Code Playgroud)
然而,这将返回1 - 尽管如此
moment().endOf('month').format('YYYY-MM-DD')
Run Code Online (Sandbox Code Playgroud)
返回正确的日期.有人可以解释为什么以及如何获得实际的一天?
我在angular 4项目中使用打字稿,并想使用moment-precise-range-plugin。
我已moment按照https://momentjs.com/docs/#/use-it/typescript/进行安装,但我想添加精确范围插件http://momentjs.com/docs/#/plugins/preciserange/。
用于Precisionrange插件的Node.js的安装位于https://codebox.org.uk/pages/moment-date-range-plugin并指定require('moment-precise-range-plugin');但未安装Typescript。
我已经尝试过import { Moment } from 'moment-precise-range-plugin';但出现以下打字稿错误
Could not find a declaration file for module 'moment-precise-range-plugin'. 'c:/Users/[UserName]/app/node_modules/moment-precise-range-plugin/moment-precise-range.js' implicitly has an 'any' type. Try npm install @types/moment-precise-range-plugin if it exists or add a new declaration (.d.ts) file containing declare module 'moment-precise-range-plugin';'
任何想法都非常欢迎!
我正在使用moment.js .fromNow()显示项目及其相对创建日期的列表,如下所示:
getRelativeDate(date: string): string {
return moment(date, 'YYYY-MM-DD HH:mm:ss').fromNow();
}
Run Code Online (Sandbox Code Playgroud)
<div class="created">{{getRelativeDate(item.created)}}</div>
Run Code Online (Sandbox Code Playgroud)
这是它的样子:
但是除非我与项目交互,否则它在初始加载后保持不变,这是有道理的,因为数据没有变化,所以当前时间是不变的。
如何强制刷新这些值?每分钟就足够了。
我有一个带有MomentJS(2.20.1)+ MomentJS(0.5.14)时区的ReactJS应用。
我想为此.js文件创建单元测试:
// dateHelper.js
import * as moment from "moment";
export function someFunc() {
// some operations with moment.utc() and moment()
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试文件:
// dateHelper.test.js
const dateHelper = require("../dateHelper");
test("format date properly", () => {
// ...
});
Run Code Online (Sandbox Code Playgroud)
但是我TypeError: moment is not a function出错了。
如何在dateHelper.js中导入momentJS以使其与Jest一起正常工作?
我想用vue-highcharts用moment.js。
<template>
<highcharts :constructor-type="'stockChart'" :options="stockOptions" :updateArgs="[true, false]"></highcharts>
</template>
<script>
...
stockOptions: {
series: [{
data: ''
}],
plotOptions: {
series: {
turboThreshold: 0
}
},
chart: {
type: 'area'
},
time: {
timezone: 'America/Sao_paulo'
}
}
...
</script>
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
Highcharts Error #25
Can't find Moment.js library
Using the global.timezone option requires the Moment.js library to be loaded.
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决此问题?
这一直困扰着我,我认为它的时间我已经掌握了它,所以有人可能会向我解释它以及如何解决它?
好吧,所以即时通讯使用片刻JS创建一个时间对象,就像这样
var startOfDiscount = moment('24/07/2018', "DD/MM/YYYY");
Run Code Online (Sandbox Code Playgroud)
当我尝试根据我的startOfDiscount对象创建一个新变量时,为什么呢?
var endOfDiscount = startOfDiscount.add(6, 'months').endOf('month');
Run Code Online (Sandbox Code Playgroud)
那改变了我的startOfDiscount对象?我知道这条线startOfDiscount.add(1, 'months').endOf('month')正在操纵实际的startOfDiscount对象,但是我该如何操作以便它只为我的新变量操作/更改,并且原始变量保持不变?
所以,如果我跑了
console.log(startOfDiscount);
console.log(endOfDiscount);
Run Code Online (Sandbox Code Playgroud)
他们都打印相同的日期?
当我尝试使用它来声明一个新对象/变量时,为什么javascript会更改原始对象/变量.我该怎么做才能对原始对象的更改仅用于设置我的新变量?
这是我从来没有理解为什么会发生以及如何防止它的事情.有人可能会解释为什么会这样做以及如何预防它?
我有一个字符串'20 -08-2018'(dd-mm-yyyy).如何将其格式化为(YYYY-MM-DD)时刻格式.当我想要格式化时,我收到了"无效日期"
moment(value).format('YYYY-MM-DD')
Run Code Online (Sandbox Code Playgroud) momentjs ×10
javascript ×9
angular ×2
date ×2
datetime ×1
ecmascript-6 ×1
highcharts ×1
jestjs ×1
jquery ×1
node.js ×1
reactjs ×1
timestamp ×1
typescript ×1
unit-testing ×1
unix ×1
vue.js ×1