discussions icon indicating copy to clipboard operation
discussions copied to clipboard

datetime error

Open lws-xzx opened this issue 1 year ago • 2 comments

Notes

  • If you have a question about the NodeSchool organization: Please open an issue in nodeschool/organizers
  • If you want to improve or contribute to workshoppers: Please open an issue in workshopper/org
  • Did you see the guide for questions? https://github.com/nodeschool/discussions#when-you-have-a-problem

Please delete this section after reading it

If you have a problem with an error message:

  • node version: <run "node -v" in you terminal and replace this>
  • npm version: <run "npm -v" in you terminal and replace this>
  • os: windows 7/windows 10/mac/linux

Error output

 你提交的结果与预期结果的比较如下:

                实际结果                                预期结果
────────────────────────────────────────────────────────────────────────────────

   "2023-09-10 17:26"                  ==    "2023-09-10 17:26"
                                       !=    ""

────────────────────────────────────────────────────────────────────────────────

 ✗

 提交的结果不符合预期!

 # 失败 你对 授时服务器 所作的答案未通过验证, 请再试一次!

My Code

 const net = require("net");

const server = net.createServer(function (socket) {
    let data = ""

    const date = new Date()
    const year = date.getFullYear().toString().padStart(4, '0')
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    const hour = date.getHours().toString().padStart(2, '0')
    const minute = date.getMinutes().toString().padStart(2, '0')

    data = year + '-' + month + '-' + day + ' ' + hour + ":" + minute

    socket.end(data)
})
server.listen(process.argv[2], () => {

})

lws-xzx avatar Sep 10 '23 09:09 lws-xzx

add '\n'

`const net = require("net");

const server = net.createServer(function (socket) { let data = ""

const date = new Date()
const year = date.getFullYear().toString().padStart(4, '0')
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
const hour = date.getHours().toString().padStart(2, '0')
const minute = date.getMinutes().toString().padStart(2, '0')

data = year + '-' + month + '-' + day + ' ' + hour + ":" + minute

socket.end(data + '\n')

}) server.listen(process.argv[2], () => {

})`

lws-xzx avatar Sep 10 '23 09:09 lws-xzx

To rectify the discrepancy, it's recommended to utilize the .trim() method to remove any potential leading or trailing whitespace from the output. This ensures an exact match between the produced output and the expected result.

const net = require("net");

const server = net.createServer(function (socket) {
    let data = "";

    const date = new Date();
    const year = date.getFullYear().toString().padStart(4, '0');
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');

    data = year + '-' + month + '-' + day + ' ' + hour + ":" + minute;

    socket.end(data.trim()); // Implementing trim() to remove possible whitespace
});

server.listen(process.argv[2], () => {});

This adjustment should resolve the discrepancy observed during the comparison.

mahdi-eth avatar Dec 29 '23 13:12 mahdi-eth