在Node.js中,如果日期之一是字符串,如何将日期与今天的日期进行比较?Node.js日期比较
var retdate = new Date();
retdate.setDate(retdate.getDate()-7);
var mydate = '2016-07-26T09:29:05.000Z'
如何比较'retdate'和'mydate',以便返回日期早于7天的日期?我相信字符串需要修改?
在Node.js中,如果日期之一是字符串,如何将日期与今天的日期进行比较?Node.js日期比较
var retdate = new Date();
retdate.setDate(retdate.getDate()-7);
var mydate = '2016-07-26T09:29:05.000Z'
如何比较'retdate'和'mydate',以便返回日期早于7天的日期?我相信字符串需要修改?
更新的解决方案:
由于OP是要求一个基于标准的JavaScript API的解决方案。
您可以做的是,由于您的日期字符串符合ISO-8601日期格式,因此您可以通过将日期字符串传递给构造函数来创建Date
对象。
一旦你得到了2 Date
对象,你可以直接从对象中减去,给你epoch
日期时间。因此,您只需要用一周(7天)内的总毫秒数来分红,以确定日期A是否比日期B早。
例子:
var retdate = new Date();
retdate.setDate(retdate.getDate()-7);
var mydatestring = '2016-07-26T09:29:05.00';
var mydate = new Date(mydatestring);
var difference = retdate - mydate; // difference in milliseconds
const TOTAL_MILLISECONDS_IN_A_WEEK = 1000 * 60 * 24 * 7;
if (Math.floor(difference/TOTAL_MILLISECONDS_IN_A_WEEK) >= 7) {
console.log("Current date is more than 7 days older than : " + mydatestring);
}
MomentJS解决方案:
像 'Newbee开发' 说MomentJS
是解决各种日期相关的一个很好的JS日期时间模块问题。
首先使用moment(...)
构造函数解析您的日期时间字符串,然后使用diff(...,'days')
API进行日期比较。
例子:
var datetime = '2016-07-26T09:29:05.000Z';
var localTime = moment();
var otherTime = moment(datetime);
console.log("Current datetime is older than " + datetime + " by 7 days = " + (localTime.diff(otherTime, 'days') >= 7));
<script src="http://momentjs.com/downloads/moment.js"></script>
感谢您的回复。我正在使用AWS Lambda。 Lambda不允许导入模块。没有使用模块,我的问题是否有工作? – tset
不熟悉AWS lambda,但您应该能够将其他节点模块推送到您的NodeJS应用程序中。阅读这篇文档:http://docs.aws.amazon.com/lambda/latest/dg/nodejs-create-deployment-pkg.html –
@tset,以防万一你无法获得momentJS安装,我有更新了我的解决方案以包含使用标准JavaScript API的解决方案。希望它有帮助,如果您遇到问题,请给我留言。 –
您可以转换日期为毫秒,做数学,并将其转换回标准的日期。
//Today's date
const today = new Date()
//Six days before today
const sixDaysAgo = new Date(+(new Date()) - 6 * 24 * 60 * 60 * 1000)
//Seven days before today
const sevenDaysAgo = new Date(+(new Date()) - 7 * 24 * 60 * 60 * 1000)
//One year ago
const oneYearAgo = new Date(+(new Date()) - 365 * 24 * 60 * 60 * 1000)
//Given date from a date string
const givenDate = new Date("2016-07-26T09:29:05.000Z")
//Convert the range of days to milliseconds
//(This wont work if the date very old)
const sevenDaysInMiliSec = 7 * 24 * 60 * 60 * 1000
var dateToValidate = sevenDaysAgo;
if (today - dateToValidate >= sevenDaysInMiliSec) {
console.log(dateToValidate + " is seven days older or more")
} else {
console.log(dateToValidate + " is less than seven days old")
}
dateToValidate = sixDaysAgo;
if (today - dateToValidate >= sevenDaysInMiliSec) {
console.log(dateToValidate + " is seven days older or more")
} else {
console.log(dateToValidate + " is less than seven days old")
}
dateToValidate = oneYearAgo;
if (today - dateToValidate >= sevenDaysInMiliSec) {
console.log(dateToValidate + " is seven days older or more")
} else {
console.log(dateToValidate + " is less than seven days old")
}
dateToValidate = givenDate;
if (today - dateToValidate >= sevenDaysInMiliSec) {
console.log(dateToValidate + " is seven days older or more")
} else {
console.log(dateToValidate + " is less than seven days old")
}
使用时刻JS模块 http://momentjs.com/ – Beginner
对不起,你是什么意思'所以它返回日期超过7天以上?' –
,你可以看到他减去7 Retdate,所以他的意思是如果想知道是否retdate比mydate抱歉太多编辑 – Beginner