将输入类型时间转换为 firestore 的时间戳
<input type="time" class="form-control job-field" id="starttime" name="starttimeadd" placeholder="Job Start Time" required>
这是我尝试过但没有运气的方法,我确实有一些尝试不同方法的结果,但显示日期或 NaN 无效。var starttimestamp = moment.utc(moment("#starttimeadd")).format();
这是预期的结果这是我的代码
function addJob(){
var starttimeadd = $('#starttimeadd').val();
const starttimestamp = moment.utc(moment(starttimeadd)).format();
db.collection("jobs").doc().set({
starttime: starttimestamp,
})
.then(function() {
console.log("Document successfully written!");
})
.catch(function(error) {
console.error("Error writing document: ", error);
});
}
最佳答案
moment
将日期作为参数,而不是像您在这里尝试做的元素 ID。怎么样:
const startTimeAdd = document.getElementById("starttimeadd").value;
const starttimestamp = moment.utc(startTimeAdd).format();
同样值得考虑的是,您可能不需要为此使用 Moment - 官方文档推荐阅读 why you may not need Moment 上的文章。
关于javascript - 将输入类型时间转换为 firestore 的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64262891/