-1

I have an API path where I need to have dates where the month is displayed as 2 digits. However, it keeps removing the zero to the left.

For example, when I get the date, if it's Jan through Sept, it only returns 1 digit. So I used the following code to add the extra zero:

const date = new Date();
  const year = date.getFullYear();
  let month = date.getMonth();
  let path = ``;
  switch (month) {
    case (month < 10):
      month = `0${month}`;
      path = `/v2/reports/time/team?from=${year}0${month}01&to=${year}0${month}31`;
      break;
    default:
      path = `/v2/reports/time/team?from=${year}${month}01&to=${year}${month}31`;
      break;
  }

However, when the code actually runs, the path is always printing as so(which returns an error, because the zeros in front of the month in the date are removed):

/v2/reports/time/team?from=2021701&to=2021731

Should be this instead:

/v2/reports/time/team?from=20210701&to=20210731

What am I missing?

Adam Norton
  • 512
  • 2
  • 5
  • 21
  • Checkout https://stackoverflow.com/a/3552493/3855179 – Sumit Surana Aug 03 '21 at 06:14
  • First suggestion - check how the [`switch` syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) is supposed to work. Yours is wrong. It's better to just use an `if`, since you only ever have two branches anyway. Might be even easier to [Javascript add leading zeroes to date](https://stackoverflow.com/q/3605214) instead of `switch`ing. – VLAZ Aug 03 '21 at 06:15
  • I will suggest you to check the length of month... if its `1` then add `0` – Mohammad Arshad Alam Aug 03 '21 at 06:15
  • Where `month < 10` you're actually adding two leading zeros, the first here: `month = \`0${month}\`` and again in `path = \`...?from=${year}0${month}01&...\`` so the server is seeing say '202100831' and likely parsing it as '2021-00-83' or worse. – RobG Aug 03 '21 at 06:19
  • @RobG if the month is less than 10, then the condition will never trigger because for `7` for example `7 === true` is `false`. – VLAZ Aug 03 '21 at 06:27
  • @VALZ—always with the details, it's the *vibe* that matters. ;-) – RobG Aug 03 '21 at 21:45

2 Answers2

0

Why don't add zero directly into variable like that?

const date = new Date();
const year = date.getFullYear();
let month = (date.getMonth() < 10) ? '0' + date.getMonth() : date.getMonth();
let path = `/v2/reports/time/team?from=${year}${month}01&to=${year}${month}31`;

console.log(path);
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
0

Switch implementation is not correct, so only default is execute, I will suggest you first explore the switch in JavaScript, this will help you in future as well,

however I have modified the code, please have a look below

const date = new Date();
const year = date.getFullYear();
let month = date.getMonth();
let path = ``;
switch (true) {
  case month < 10:
    path = `/v2/reports/time/team?from=${year}0${month}01&to=${year}0${month}31`;
    break;
  default:
    path = `/v2/reports/time/team?from=${year}${month}01&to=${year}${month}31`;
    break;
}
Suneel Kumar
  • 756
  • 2
  • 5
  • 11