gpt4 book ai didi

javascript - Moment.js 不接受查询字符串作为有效输入

转载 作者:可可西里 更新时间:2023-11-01 16:41:20 25 4
gpt4 key购买 nike

我有以下 GET 函数,可将查询字符串转换为时刻(或日期)对象。我运行了一系列测试,基本上 moment.js 似乎不接受 req.query.m 作为有效字符串。如果我对输入进行硬编码,Moment 工作正常,并且我能够获取查询字符串本身。

查询字符串是:

http://localhost:8080/file/newCalendar?m=1455910939

app.get('/file/newCalendar', function(req, res) {
var dateSelect = req.query.m;
var selectedDate = moment(req.query.m);
var selectedMonth = selectedDate.month();

jsontest = {
'testQuery': dateSelect,
'testMomentOnQuery': moment(req.query.m),
'testMomentOnVariable': moment(dateSelect),
'testMomentMonth': moment(12345678).month(),
};

res.json(jsontest);
})

我也尝试过使用 Date() 而不是 moment。

 app.get('/file/newCalendar', function(req, res) {
var selectedDate = Date(req.query.m);
var selectedMonth = selectedDate.getMonth();

jsontest = {
'selectedDate': selectedDate,
'selectedMonth': selectedMonth,
};

res.json(jsontest);
})

Date() 工作正常,但 .getMonth() 函数获取

TypeError: selectedDate.getMonth is not a function at Object.handle

提前致谢!

最佳答案

尝试使用 moment(parseInt(req.query.m))

你也可以使用 moment(req.query.m, 'x')第二个参数是格式字符串。自 1970 年以来 X 代表秒,x 代表毫秒。(unix ts)

关于javascript - Moment.js 不接受查询字符串作为有效输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35712633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com