1

I have an API call that returns an array of json strings. I need to change the format for the purposes of display - just the month and day is needed.

My logic has a bug because it's translating August as "7" instead of "8". also the day is being interpretted as 3 instead of 25 for the first record.

Sample Json Data

This is a sample of what the data looks like:

[
{"reportRefreshDate":"2021-08-25T07:18:06","Widgets":13,"Gizmos":6},{"reportRefreshDate":"2021-08-28T07:18:06","Widgets":1,"Gizmos":0},{"reportRefreshDate":"2021-08-29T07:18:06","Widgets":1,"Gizmos":0}
]

Code

Here's the relevant code:

  const reportRefreshDate: string[] = [];
  const totalWidgets: number[] = [];
  const totalGizmos: number[] = [];
  const totalDooDads: number[] = [];

  for (const record of data) {
    const tempdate = new Date(record.reportRefreshDate);

    reportRefreshDate.push(tempdate.getMonth() + "/" + tempdate.getDay())
  }

In a debug window, this is what I've tried:

tempdate
Wed Aug 25 2021 07:18:06 GMT-0400 (Eastern Daylight Time)

tempdate.getMonth()
7

If you could point me in the right direction, that'd be great. Right now i'm also looking at the overloads for Date to see if the way I created the variable is incorrect.

EDIT 1

I guess I could just convert to localeDateString and then strip off the year? I'm noticing that when I use the toLocaleDateString() it returns the right values in my debug console:

tempdate.toLocaleDateString()
'8/25/2021'
dot
  • 14,928
  • 41
  • 110
  • 218
  • 1
    Months in JS start from zero. `0` is January, thus `7` is August. Simply add 1 to convert to the regular count. – VLAZ Dec 31 '21 at 15:56
  • doh! thanks man! I forgot to mention that the day is also wrong. 8/25 is coming back as 8/3. Any tips? Updating my post – dot Dec 31 '21 at 15:57
  • Does this answer your question? [getMonth in javascript gives previous month](https://stackoverflow.com/questions/18624326/getmonth-in-javascript-gives-previous-month) – VLAZ Dec 31 '21 at 15:58
  • @VLAZ only in part. – dot Dec 31 '21 at 16:03
  • Does this answer your question? [Parsing a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/parsing-a-string-to-a-date-in-javascript) – voiarn Dec 31 '21 at 16:07
  • 1
    @VLAZ seems like you confused `getDay()` function with `getDate()` function. In order to get correct day of month, you should use `getDate()` function. `getDay()` function returns day of the week as a number starting from 0. That's the reason why you get 3 as day. 3 means Wednesday. – Alan Z Dec 31 '21 at 16:07
  • @voiarn I don't see how it's relevant. It's already parsed. – VLAZ Dec 31 '21 at 16:08
  • @AlanZhou ??? where have I mentioned either of these? – VLAZ Dec 31 '21 at 16:08
  • @VLAZ @dot `reportRefreshDate.push(tempdate.getMonth() + "/" + tempdate.getDay())`. Instead use `reportRefreshDate.push(tempdate.getMonth() + "/" + tempdate.getDate())`. – Alan Z Dec 31 '21 at 16:09
  • 1
    @AlanZhou in what way shape or form did I write this question? – VLAZ Dec 31 '21 at 16:10
  • @VLAZ sorry, I thought you were the original poster. – Alan Z Dec 31 '21 at 16:12

1 Answers1

2

You shouldn't use getDay() function which will return the day of week as a number starting from 0.

Instead, you should use getDate() function which will return the day of month. So you might need to refactor your code to the following.

reportRefreshDate.push(tempdate.getMonth() + "/" + tempdate.getDate())
Alan Z
  • 803
  • 1
  • 7
  • 13