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
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
Share
Improve this question
edited Mar 12, 2019 at 12:01
SmartestVEGA
asked Mar 11, 2019 at 12:25
SmartestVEGASmartestVEGA
8,92928 gold badges95 silver badges150 bronze badges
5
- No even i have many records greater than and less thanDate('3/11/2019 7:29:12 AM') none of them filter , always return zero – SmartestVEGA Commented Mar 11, 2019 at 12:32
- 1 Did you check the date in array after converting it to string value? – Thamarai T Commented Mar 11, 2019 at 12:35
- LogTime:"Mon Mar 11 2019 07:39:12 GMT+0530 (India Standard Time)" this is the value after converting – SmartestVEGA Commented Mar 11, 2019 at 12:37
-
2
What absolute time are you expecting
new Date('3/11/2019 7:29:12 AM')
to be? It doesn't indicate a time zone. – Mark Commented Mar 11, 2019 at 12:39 - its client side scrpting so zone doesnt matter i hope – SmartestVEGA Commented Mar 12, 2019 at 12:01
2 Answers
Reset to default 2Most of your problem here consist of the date time format not being ISO 8601 pliant. 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. Then you can use moment.js
(https://momentjs.), which is a must for handling time in 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./downloads/moment.min.js"></script>
Code when not using 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:
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.
I personally use Moment Timezone to handle these cases.
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)