Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datetime error #2911

Open
lws-xzx opened this issue Sep 10, 2023 · 2 comments
Open

datetime error #2911

lws-xzx opened this issue Sep 10, 2023 · 2 comments

Comments

@lws-xzx
Copy link

lws-xzx commented Sep 10, 2023

Notes

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
Copy link
Author

lws-xzx commented Sep 10, 2023

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], () => {

})`

@mahdi-eth
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants