Using moment.js to find the index of an object matching today's date

I am trying to find the index of an object in an array that contains a timestamp that matches the current date. Today is the 27th, so based off of my dataset, it should return the first index. I have tried two solutions, neither of which work for different reasons. I am open to using a solution to either method.

I have tried using an exact string match.

const index = _.findIndex(data, function(day) { 
   return day.timestampUtc === moment().startOf('day').toISOString() })

The above does not work because it is trying to match 2021-01-27T00:00:00Z to 2021-01-27T06:00:00.000Z which never catches because it would return undefined.

I have also tried

const index = _.findIndex(data, function(day) { 
   return moment(day.timestampUtc).day() === moment().day() }) //should return 1, but returns 2

This one didn’t work because using moment(day.timestampUtc) would evaluate it to the day before due to timezone offset, so my returned index would be one too high because it evaluates against one day early.

data

[{
  timestampUtc: "2021-01-26T00:00:00Z"
  weatherCodeType: "FLURRIES"
 },
 {
  timestampUtc: "2021-01-27T00:00:00Z"
  weatherCodeType: "RAIN"
 }, //I want this one
{
  timestampUtc: "2021-01-28T00:00:00Z"
  weatherCodeType: "SNOW"
 },
]

146 thoughts on “Using moment.js to find the index of an object matching today's date”

  1. Pingback: viagra 100mg
  2. Pingback: sale cialis
  3. Pingback: canada viagra
  4. Pingback: natural viagra
  5. Pingback: viagra and cialis
  6. Pingback: viagra otc
  7. Pingback: tadalafil prix
  8. Pingback: boots
  9. Pingback: sildenafil 20 mg
  10. Pingback: sildenafil viagra
  11. Pingback: whats viagra
  12. Pingback: viagra cvs
  13. Pingback: herbal viagra
  14. Pingback: viagra gum
  15. Pingback: viagra pill
  16. Pingback: otc viagra
  17. Pingback: viagra pills
  18. Pingback: viagra for sale
  19. Pingback: sildenafil pills
  20. Pingback: viagra or cialis
  21. Pingback: cialis women
  22. Pingback: cialis dosis

Leave a Comment