The last line of your code won't return a thing, if these conditions are not met:
- the `Created` property of a specific `JSON` element of your array must be greater than or equal to `firstShiftStart` (`2022-12-21 08:00:00`, in your code)
- the `Created` property of that same `JSON` element of your array must be less than or equal to `firstShiftEnd` (`2022-12-21 14:00:00`, in your code)
- finally, the `Choice` property of that same `JSON` should be equal to `Like`
Since we know only one element of your array of `JSON` objects, we can see that one of the conditions is not met - the timestamp is before `firstShiftStart`, and your filter will remove it from the array.
Also, there are some syntax errors in your code - leading zeroes, double zeroes, etc.
I've taken the liberty of fixing it up a bit (not entirely), and changing your filtering in the last line, so that you may better understand why your code isn't returning anything.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let current_datetime = new Date();
var firstShiftStart = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " 08:00:00";
var firstShiftEnd = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " 14:00:00";
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
var dataAllJson = [
{
"Choice": "Like",
"Created": "2022-12-22 11:50:04",
"Id": 1
},
{
"Choice": "dislike",
"Created": "2022-12-22 11:44:04",
"Id": 2
},
{
"Choice": "dislike",
"Created": "2022-12-22 07:50:04",
"Id": 3
},
{
"Choice": "dislike",
"Created": "2022-12-22 07:50:04",
"Id": 4
},
{
"Choice": "dislike",
"Created": "2022-12-22 07:50:04",
"Id": 5
},
{
"Choice": "Like",
"Created": "2022-12-22 07:50:04",
"Id": 6
},
];
var votesforDay = dataAllJson.filter(x => x.Created >= today);
var likesFirstShift = votesforDay.filter(function(x) {
if(x.Created >= firstShiftStart && x.Created <= firstShiftEnd && x.Choice == "Like") {
console.log(x.Id);
console.log(x.Created + " >= " + firstShiftStart);
console.log(x.Created + " <= " + firstShiftEnd);
console.log(x.Choice + " == Like");
}
return x.Created >= firstShiftStart && x.Created <= firstShiftEnd && x.Choice == "Like"
}).length;
console.log("=============================");
console.log(likesFirstShift);
<!-- end snippet -->
Some things to note:
- `firstShiftStart` and `firstShiftEnd` are defined in a slightly different manner. Note the differences between the example and what was in your code
- I've changed the date to todays (22nd of December 2022), so that you can test the code
- I have also filled `dataAllJson` with semi-random data, again, for testing purposes
- `votesForDay.filter(...)` has been expanded to a slightly different notation, for testing purposes (note the `console.log`ging)
Use `new Date()` to generate a new `Date` object containing the current date and time.
<!-- begin snippet: js hide: false -->
<!-- language: lang-js -->
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
document.write(today);
<!-- end snippet -->
This will give you today's date in the format of mm/dd/yyyy.
Simply change `today = mm +'/'+ dd +'/'+ yyyy;` to whatever format you wish.