I am working with discord.js and Clash Of clans API to make a discord bot.
I made several functions but I can’t get 1 to function properly. Here is the code:
const clanwar = JSON.parse(body);
const prepStart = new Date(clanwar.preparationStartTime.slice(0, -8));
const prepStartStr = `${prepStart.getDate()}/${prepStart.getMonth() + 1}/${prepStart.getFullYear()} ${prepStart.getHours()} ${prepStart.getMinutes()} ${prepStart.getSeconds()}`;
const start = new Date(clanwar.startTime.slice(0, -8));
const startStr = `${start.getDate()}/${start.getMonth() + 1}/${start.getFullYear()} ${start.getHours()} ${start.getMinutes()} ${start.getSeconds()}`;
const end = new Date(clanwar.endTime.slice(0, -8));
const endStr = `${end.getDate()}/${end.getMonth() + 1}/${end.getFullYear()} ${end.getHours()} ${end.getMinutes()} ${end.getSeconds()}`;
if (err)
{
callback(err);
return;
};
if (res.statusCode === 404)
{
const error = new Error(`Make sure you've provided a tag without # : n :x: `.clan #XYZ12345`n:white_check_mark: `.clan XYZ12345`nIf the command still doesn't work contact staff and/or report using the .report (message) command`);
callback(error);
return;
};
callback(null, { clanwar, prepStartStr, startStr, endStr });
The code is messy, I know and isn’t optimized but I will work on that Later. The issue right now is on the line where I get the prepStartStr StartStr EndStr where I get the date from the Body which comes in this form:
preparationStartTime: ‘20230422T185511.000Z’,
startTime: ‘20230423T175511.000Z’,
endTime: ‘20230424T175511.000Z’,
But when I run it, it displays the time like this and console.log() it this is what I get in the console:
Prep: NaN/NaN/NaN NaN NaN NaN
Start: NaN/NaN/NaN NaN NaN NaN
End: NaN/NaN/NaN NaN NaN NaN
Full Code for the funtion and where I call it:
https://pastecode.io/s/isfkvp2h
Console Output
https://i.stack.imgur.com/GhIsv.png
Firstly, I used the toISOString() method to convert the dates to string format, which resulted in an incorrect output of NaN/NaN …
I then tried modifying the code to use the toLocaleString() method instead, but this also produced an incorrect output. Afterwatds, I attempted to replace the T and .000Z substrings in the date strings before parsing them, but this still didn’t fix the issue. Finally, I modified the code to use the slice() method to remove the last five characters from the date strings instead of replacing substrings, but this still resulted in an incorrect output.
At this point, i’m still trying to figure out why the output is incorrect and how to fix it.
I tried chatGPT which also tried similar things and i still can’t get it to work