使用Moment.js我无法将正确的时刻对象转换为带时区的日期对象.我无法得到正确的日期.
例:
var oldDate = new Date(),
momentObj = moment(oldDate).tz("MST7MDT"),
newDate = momentObj.toDate();
console.log("start date " + oldDate)
console.log("Format from moment with offset " + momentObj.format())
console.log("Format from moment without offset " + momentObj.utc().format())
console.log("(Date object) Time with offset " + newDate)
console.log("(Date object) Time without offset "+ moment.utc(newDate).toDate())
Run Code Online (Sandbox Code Playgroud) 我可以使用MomentJs获得两个日期之间的差异,如下所示:
moment(end.diff(startTime)).format("m[m] s[s]")
Run Code Online (Sandbox Code Playgroud)
但是,我也希望在适用时显示小时(仅当> = 60分钟时才显示).
但是,当我尝试使用以下内容检索持续时间时:
var duration = moment.duration(end.diff(startTime));
var hours = duration.hours();
Run Code Online (Sandbox Code Playgroud)
它返回当前小时而不是两个日期之间的小时数.
如何获得两个时刻之间的小时差异?
我正在使用moment.js格式化我的日期时间,这里我有两个日期值,并且我希望在一个日期大于另一个日期时实现特定功能.我阅读了他们的大部分文档,但没有找到实现这一目标的功能.我知道它会在那里.
这是我的代码
var date_time = 2013-03-24 + 'T' + 10:15:20:12 + 'Z'
var d = moment(date_time).tz('UTC'); // first date
var now = new Date(),
dnow = moment(now).tz('UTC'),
snow = dnow.minute() % 15,
diffnow = 15 - snow,
tonow = moment(dnow).add('minute', diffnow),
ahead30now = moment(tonow).add('minute', 30);
if (d > ahead30now) {
// allow input time
console.log('UTC TIME DB', d.format());
} else {
}
Run Code Online (Sandbox Code Playgroud)
编辑
var date_time = req.body.date + 'T' + req.body.time + req.body.timezone; // 2014-03-24T01:15:000
var utc_input_time = moment(date_time).utc(); // …Run Code Online (Sandbox Code Playgroud) formatCalendarDate = function (dateTime) {
return moment.utc(dateTime).format('LLL');
};
Run Code Online (Sandbox Code Playgroud)
它显示:"28 februari 2013 09:24"
但我想在最后删除时间.我怎样才能做到这一点?
我正在使用Moment.js.
我有一个这种格式的字符串:
var testDate = "Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)"
我想使用moment.js以这种格式mm/dd/yyyy : 04/12/2013显示它.
我尝试使用这种方法,
moment(testDate,'mm/dd/yyyy');
Run Code Online (Sandbox Code Playgroud)
哪个错误并说there is no such method called replace?我是以错误的方式接近这个吗?
编辑:
我还要提一下,我正在使用为meteor.js打包的预打包版本的moment.js
Object [object Date] has no method 'replace' : The Exact error from the console
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
at makeDateFromStringAndFormat (http://127.0.0.1:3000/packages/moment/lib/moment/moment.js?b4e3ac4a3d0794023a4410e7941c3e179398b5b0:539:29)
at moment (http://127.0.0.1:3000/packages/moment/lib/moment/moment.js?b4e3ac4a3d0794023a4410e7941c3e179398b5b0:652:24)
at populateProfileForEdit (http://127.0.0.1:3000/client/views/home/administration/directory/profiles/profiles.js?acfff908a6a099f37312f62892a22b40f82e5e0f:147:25)
at Object.Template.profile_personal.rendered (http://127.0.0.1:3000/client/views/home/administration/directory/profiles/profiles.js?acfff908a6a099f37312f62892a22b40f82e5e0f:130:13)
at Spark.createLandmark.rendered (http://127.0.0.1:3000/packages/templating/deftemplate.js?b622653d121262e50a80be772bf5b1e55ab33881:126:42)
at http://127.0.0.1:3000/packages/spark/spark.js?45c746f38023ceb80745f4b4280457e15f058bbc:384:32
at Array.forEach (native)
at Function._.each._.forEach (http://127.0.0.1:3000/packages/underscore/underscore.js?867d3653d53e9c7a171483edbcad9670e12288c7:79:11)
at http://127.0.0.1:3000/packages/spark/spark.js?45c746f38023ceb80745f4b4280457e15f058bbc:382:7
at _.extend.flush (http://127.0.0.1:3000/packages/deps/deps.js?9642a93ae1f8ffa8eb1c2475b198c764f183d693:231:11)
Run Code Online (Sandbox Code Playgroud) 我知道我可以做任何事情,还有一些更多关于日期的日期.但令人尴尬的是,我很难尝试做一些看起来很简单的事情:将两次之间的差异搞定.
例:
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) 我试图用来momentjs检查给定日期是今天还是将来.
这是我到目前为止:
<script type="text/javascript" src="http://momentjs.com/downloads/moment.min.js"></script>
<script type="text/javascript">
var SpecialToDate = '31/01/2014'; // DD/MM/YYYY
var SpecialTo = moment(SpecialToDate, "DD/MM/YYYY");
if (moment().diff(SpecialTo) > 0) {
alert('date is today or in future');
} else {
alert('date is in the past');
}
</script>
Run Code Online (Sandbox Code Playgroud)
该代码正在评估我的日期(2014年1月31日)作为过去的日期.
知道我做错了什么吗?
无论如何,当你使用webpack时,你可以阻止moment.js加载所有语言环境(我只需要英语)吗?我正在查看源代码,如果定义了hasModule,它是用于webpack,那么它总是尝试require()每个语言环境.我很确定这需要拉动请求来修复.但无论如何我们可以使用webpack配置修复此问题.
这是我的webpack配置加载momentjs
resolve: {
alias: {
moment: path.join(__dirname, "src/lib/bower/moment/moment.js")
},
},
Run Code Online (Sandbox Code Playgroud)
然后我需要它的任何地方我只需要('时刻')这可行,但它添加了大约250kb的不需要的语言文件到我的包.此外,我正在使用bower版本的momentjs和gulp.
此外,如果这不能通过webpack配置修复,这里是一个链接到它加载语言环境的函数https://github.com/moment/moment/blob/develop/moment.js#L760-L772我尝试添加"&& module.exports.loadLocales"到if语句,但是我认为webpack并不能以某种方式工作它只需要我认为它现在使用正则表达式所以我真的不知道你会怎么做去解决它.无论如何,谢谢你的帮助.
有什么办法可以在持续时间对象上使用moment.js format方法吗?我在文档中的任何地方都找不到它,并且它看起来不是持续时间对象的属性.
我希望能够做到这样的事情:
var diff = moment(end).unix() - moment(start).unix();
moment.duration(diff).format('hh:mm:ss')
Run Code Online (Sandbox Code Playgroud)
此外,如果有任何其他库可以轻松容纳这种功能,我会对reccomendations感兴趣.
谢谢!
这篇文档moment.ISO_8601作为格式化选项提供(来自2.7.0 - http://momentjs.com/docs/#/parsing/special-formats/),但这些都不起作用(甚至2.7.0):
var date = moment();
date.format(moment.ISO_8601); // error
moment.format(date, moment.ISO_8601); // error
Run Code Online (Sandbox Code Playgroud)
(http://jsfiddle.net/b3d6uy05/1/)
如何从moment.js获得ISO 8601?
momentjs ×10
javascript ×9
date ×3
date-parsing ×1
datetime ×1
difference ×1
gulp ×1
iso8601 ×1
jquery ×1
time ×1
webpack ×1