[英]How to convert the DD-MM-YYYY HH:mm format to epoch time using moment or js?
I have a date time picker and it omits date in format of a DD-MM-YYYY HH:mm a
format.我有一个日期时间选择器,它以
DD-MM-YYYY HH:mm a
格式省略日期。 I want to convert it into epoch time to store it in the database backend.我想将其转换为纪元时间以将其存储在数据库后端。 I have tried using the following methods so far, but all of them return
NaN
as output:到目前为止,我已经尝试使用以下方法,但它们都将
NaN
返回为 output:
var loadingPlannedUnTime = this.state.loadingPlannedUnTime;
console.log("normal conversion"+loadingPlannedUnTime);
//returns 9-08-2020 15:08:00
//Moment method
var loadingPlannedUnTime = moment(this.state.loadingPlannedUnTime).unix();
console.log("moemnt"+loadingPlannedUnTime)
//Returns NaN
//JS getTime() method
var pickupDateTime = new Date(this.state.loadingPickupTime);
var loadingPickupTime = pickupDateTime.getTime();
console.log("new date and gettime"+loadingPickupTime)
//Returns NaN
What is the correct method to convert it to epoch time?将其转换为纪元时间的正确方法是什么?
https://momentjs.com/docs/ https://momentjs.com/docs/
If you know the format of an input string, you can use that to parse a moment.如果您知道输入字符串的格式,您可以使用它来解析片刻。
moment("12-25-1995", "MM-DD-YYYY");
const x = moment('24-12-2019 09:15', "DD-MM-YYYY hh:mm"); console.log(x.format())
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
const matches = loadingPlannedUnTime.match(/(\d{1,2})-(\d{1,2})-(\d{2,4}) (\d{1,2}):(\d{1,2}):(\d{1,2})/);
if (!!matches) {
// new Date(year, month, day, hours, minutes, seconds, milliseconds)
const epoch = new Date(matches[3], matches[2] - 1, matches[1], matches[4], matches[5], matches[6]).getTime();
console.log(epoch);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.