Moment.js执行人性化和日历日期,例如:
moment().calendar()
"Today at 10:17 AM"
Run Code Online (Sandbox Code Playgroud)
获取当前日期对象并转换为日历日期,和
有没有办法反过来呢?就像我给它一样"Today at 10:17 AM",它会返回一个日期对象,其中包含今天的日期和上午10:17的时间?
您好我想问一下,我怎么能在Javascript或Javascript库中做到这一点Moment.js http://momentjs.com/docs/(我更喜欢这个库)以下:
Today is date (for example): 18-09-2014
Run Code Online (Sandbox Code Playgroud)
我需要从给定的日期获得实际周的范围(从星期一到星期日),这意味着对于给定的日期我应该得到结果:
start: 15-09-2014
end: 21-09-2014
Run Code Online (Sandbox Code Playgroud)
有可能,我该怎么办?
谢谢你的帮助.
我想通过momentjs从约会到现在.
我尝试过fromNow().该方法返回值为字符串.
它的单位变化取决于范围的长度.
我正在研究一个本机应用程序,我正在尝试使用片刻格式化日期.
日期看起来像"02-16-2016 09:04:23"
function formatTime(date){
var formattedDate = moment(date).format('MM:ss A');
return formattedDate;
}
Run Code Online (Sandbox Code Playgroud)
如果chrome调试器处于活动状态,则工作正常.但如果我禁用它,我得到的只是"无效日期"
与我正在使用的解码功能相同
var that = this;
MessagesService.getMessageBody(selectedMessage)
.then(function(messageBody){
var decodedData = window.atob(messageBody.messages);
that.setState({
messageBody: decodedData
})
})
.catch(function(err){
console.log(err);
})
Run Code Online (Sandbox Code Playgroud)
用,显示解码数据
<Text> Body: {this.state.messageBody} </Text>
Run Code Online (Sandbox Code Playgroud)
并显示日期
<View style = {[MessageStyles.senderItem, MessageStyles.date]}>
<Text>
{this.formatTime(message.createDateTime)}
</Text>
</View>
Run Code Online (Sandbox Code Playgroud)
也许这是一个糟糕的方式来做反应本机?还在学习,所以我可能会做一些不好的练习.
我正在使用moment.js而我无法更改默认语言环境:
moment.locale('fr');
console.log(moment.locale()); // en
Run Code Online (Sandbox Code Playgroud) 我的目标是建立像Snapchat这样的功能,从现在起24小时后我发布的内容将被删除.我想创建一个API,用于检查当前时间是否已超过24小时,如果超过,则将删除它,它将检查创建的时间和当前时间,并将在这两个时间之间进行比较.
我用时间将日期/时间存储到数据库.假设我的数据库代码如下所示
var mongoose = require('mongoose');
var moment = require('moment');
var ItemPost = new Schema({
name: String,
created: { type: Date, default: moment() },
timeLeft: { type: Number, default: 24 },
});
Run Code Online (Sandbox Code Playgroud)
例如,如果我现在发布内容
11:31 PM 8 Jan 2017
Run Code Online (Sandbox Code Playgroud)
假设现在的时间是上述时间.比方说9个小时过去了,现在的时间是
08:31 PM 9 Jan 2017
Run Code Online (Sandbox Code Playgroud)
如何在创建日期后24小时内获得时间差异?
我的尝试
function checkTime(item) {
var currentTime = moment();
// assume item is an ItemPost object from the database
var timeStore = item.created()
// Im stuck here, I tried various codes. But nothing has worked so …Run Code Online (Sandbox Code Playgroud) 我怎样才能将字符串转换"8/3/2017 6:12:00 AM"到"2017-08-03T06:12:00.000"使用标准的JavaScript或moment.js库?
好了,我看到检查的Object是moment我需要检查与方法moment.isMoment(obj),但是当我尝试验证moment().toDate()它告诉我,这是假的.
我如何检查对象moment是否是它.toDate()?
moment.isMoment(moment()) // true
moment.isMoment(moment().toDate()) // false
Run Code Online (Sandbox Code Playgroud) 当我在这样的方法上花点时间时:
<template>
...
</template>
<script>
export default{
...
methods:{
...
add(event){
let current = moment()
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
然后调用add方法,它可以正常工作。
但是,如果我像这样在坐骑上放一些力矩:
mounted(){
let currentAt = moment()
}
Run Code Online (Sandbox Code Playgroud)
这是行不通的。它返回以下错误:
[Vue警告]:安装的挂钩中出现错误:“ ReferenceError:未定义力矩”
我该如何解决?
我用角6进口了一下:
import moment from 'moment'
Run Code Online (Sandbox Code Playgroud)
和
let formatted = moment(startDate).tz(timezone).format();
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常...但ng服务输出以下错误:
Module '"/my/project/node_modules/moment/moment"' has no default export.
Run Code Online (Sandbox Code Playgroud)
好的,我理解这个错误; 它说那个时刻不会被导出为默认值...但是当我尝试以这种方式导出时:
import { moment } from 'moment'
Run Code Online (Sandbox Code Playgroud)
但是,错误说,那个时刻实际上是一个对象,而不是一个功能.
它的第一步是如何工作的呢?我应该如何导入"时刻"?
更新 我也按照其他问题的建议尝试了......
import * as moment from 'moment';
Run Code Online (Sandbox Code Playgroud)
但是我得到了以下结果:
ERROR in node_modules/@angular/core/src/render3/ng_dev_mode.d.ts(9,11): error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'.
node_modules/angular-moment-timezone/node_modules/@angular/core/src/render3/ng_dev_mode.d.ts(9,11): error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'.
Run Code Online (Sandbox Code Playgroud) momentjs ×10
javascript ×7
node.js ×2
angular ×1
angularjs ×1
date ×1
date-range ×1
datetime ×1
mongoose ×1
parsing ×1
react-native ×1
vue.js ×1
vuejs2 ×1
vuex ×1