简体   繁体   English

用Javascript过滤日期

[英]Filtering Date in Javascript

I am trying to filter an array which contain date(converted from string) which is greater than a particular date.. 我试图过滤包含大于特定日期的日期(从字符串转换为日期)的数组。

_this.downloadData.forEach(_d => _d.LogTime = _d.LogTime.toString());

console.log(_this.downloadData.filter(x=>new Date(x.LogTime) > new Date('3/11/2019 7:29:12 AM')));

But filter always return zero items 但是过滤器总是返回零项

.

Array looks like below: 数组如下所示:

[0 … 99]
0:
CPUStat:""
Connectivity:""
DiskStatus:""
HostName:"HOSTname"
LastRebootStatus:null
LogTime:"Mon Mar 11 2019 07:39:12 GMT+0530 (India Standard Time)"

__proto__:
Object

Most of your problem here consist of the date time format not being ISO 8601 compliant. 您在这里遇到的大多数问题是日期时间格式不符合ISO 8601。 Because of this you need to clean up the date. 因此,您需要清理日期。 LogTime.substring(4,23) + obj.LogTime.substring(28,33) will get the important parts from your string. LogTime.substring(4,23) + obj.LogTime.substring(28,33)将从您的字符串中获取重要部分。 Then you can use moment.js ( https://momentjs.com ), which is a must for handling time in JavaScript. 然后,您可以使用moment.jshttps://momentjs.com ),这是在JavaScript中处理时间所必需的。

You can run the code below and see that it works 您可以运行下面的代码,然后查看它是否有效

 // Your example time array let timeArray = [ { LogTime:"Mon Mar 11 2019 07:39:12 GMT+0530 (India Standard Time)" }, { LogTime:"Mon Mar 11 2019 08:39:12 GMT+0530 (India Standard Time)" }, { LogTime:"Mon Mar 11 2019 09:39:12 GMT+0530 (India Standard Time)" }, { LogTime:"Mon Mar 11 2019 10:39:12 GMT+0530 (India Standard Time)" } ], format = "MMM MM YYYY HH:mm:ssZ", shouldBeAfter = moment('Mar 11 2019 09:00:12+0530', format), result, filterDateGreaterThan = function (obj){ let sDate, mDate; sDate = obj.LogTime.substring(4,23) + obj.LogTime.substring(28,33); mDate = moment(sDate, format); // console.log(mDate); return mDate.isAfter(shouldBeAfter); }; result = timeArray.filter(filterDateGreaterThan); console.log(result); 
 <script src="https://momentjs.com/downloads/moment.min.js"></script> 

Code when not using Moment.js 不使用Moment.js时的代码

 // Your example time array let timeArray = [ { LogTime:"Mon Mar 11 2019 07:39:12 GMT+0530 (India Standard Time)" }, { LogTime:"Mon Mar 11 2019 08:39:12 GMT+0530 (India Standard Time)" }, { LogTime:"Mon Mar 11 2019 09:39:12 GMT+0530 (India Standard Time)" }, { LogTime:"Mon Mar 11 2019 10:39:12 GMT+0530 (India Standard Time)" } ], shouldBeAfter = new Date('Mar 11 2019 09:00:12+0530'), result, filterDateGreaterThan = function (obj){ let sDate, date; sDate = obj.LogTime.substring(4,23) + obj.LogTime.substring(28,33); date = new Date(sDate); return date.valueOf() > shouldBeAfter.valueOf(); }; result = timeArray.filter(filterDateGreaterThan); console.log(result); 

You should be able to fix your problem by adding a timezone to your input. 您应该可以通过在输入中添加时区来解决问题。 If you don't provide a timezone, your browser will use your local timezone. 如果您不提供时区,则浏览器将使用您的本地时区。 For instance, my timezone is PDT, so I end up seeing this: 例如,我的时区是PDT,所以最终看到了以下内容:

Without Timezone in input:
input  : 3/11/2019 7:29:12 AM
output : Mon Mar 11 2019 07:29:12 GMT-0700 (Pacific Daylight Time)

With Timezone in input:
input  : Mon Mar 11 2019 07:39:12 GMT+0530 (India Standard Time)
output : Sun Mar 10 2019 19:09:12 GMT-0700 (Pacific Daylight Time)

Note that with the timezone in your input, the unix timestamp matches the output. 请注意,在您输入的时区中,Unix时间戳与输出匹配。


I personally use Moment Timezone to handle these cases. 我个人使用Moment Timezone处理这些情况。

Without Timezone in input:
input  : moment.tz("3/11/2019 7:29:12 AM", "Asia/Calcutta").toDate();
output : Mon Mar 11 2019 00:29:12 GMT-0700 (Pacific Daylight Time)

With Timezone in input:
input  : moment.tz("Mon Mar 11 2019 07:39:12 GMT+0530 (India Standard Time)", "Asia/Calcutta").toDate();
output : Mon Mar 11 2019 00:39:12 GMT-0700 (Pacific Daylight Time)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM