0

I tried to make a script that takes a row form a sheet full with dates, convert it into an array and count a specific date.

I managed to do all except the "count a specific date" part.

the dates in the array looks like this:

Fri Jan 27 2017 00:00:00 GMT+0100 (MEZ)

The arry:

Fri Jan 27 2017 00:00:00 GMT+0100 (MEZ),Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ),Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ),Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ),Mon Jan 02 2017 00:00:00 GMT+0100 (MEZ),,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

The wanted Output: (for "Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ)"

3
3
  • 1
    Just write a simple loop that compares the array element and increments a counter. Or use array.filter(d => d == specific_date).length Commented Feb 3, 2017 at 17:07
  • @Barmar I think this should be tagged arrays? Should I make the edit? Commented Feb 3, 2017 at 17:08
  • 1
    Post an example of the array and the desired output of that array. Commented Feb 3, 2017 at 17:10

2 Answers 2

1

var array = ["Fri Jan 27 2017 00:00:00 GMT+0100 (MEZ)", "Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ)", "Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ)", "Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ)", "Mon Jan 02 2017 00:00:00 GMT+0100 (MEZ)"];

var dateToCount = "Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ)";

var count = array.reduce(function (a, d) {
    return d == dateToCount ? ++a : a;
}, 0);

console.log(count);

Note: If you could have lowercases or other form of dates and want to get those counted as well, then parse the dates like this:

var dateToCount = Date.parse("Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ)");
var count = array.reduce(function (a, d) {
    return Date.parse(d) == dateToCount ? ++a : a;
}, 0);
Sign up to request clarification or add additional context in comments.

5 Comments

the third line gives me a Syntax error in Google Scripts
@SteffenBauer It could be that your browser doesn't suppot arrow functions. I updated my answer.
@SteffenBauer That's not an error related to my answer. My code works. Run it in a browser not in an emulator like in the link you provided! It sais the syntax error is in the console.log not in my code above it.
i think somebody deleted the my "Google Apps Script" Tag for this post -.-
@SteffenBauer Just change the line console. log(count) to whatever does show count. Logger.log("Count is: " + count) and check your logs for example!
0

If you have an array of strings like these:

var arr = ["Fri Jan 27 2017 00:00:00 GMT+0100 (MEZ)", 
           "Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ)", 
           "Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ)", 
           "Wed Jan 11 2017 00:00:00 GMT+0100 (MEZ)", 
           "Mon Jan 02 2017 00:00:00 GMT+0100 (MEZ)"];`

Use 2 strings of code;

var day = new Date(arr[0]);
day = day.getDay() + 1;

Just read docs of Date object:

Date object itself: http://www.w3schools.com/jsref/jsref_obj_date.asp

Date.getDay() method: http://www.w3schools.com/jsref/jsref_getday.asp

1 Comment

@ppasler, thanx. Fixed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.