简体   繁体   English

如何使用 moment 或 js 将 DD-MM-YYYY HH:mm 格式转换为纪元时间?

[英]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.

相关问题 如何使用JavaScript将数据格式“ YYYY-mm-dd hh:mm:ss”转换为“ dd-mm-YYYY hh:mm:ss”? - How to convert data format “YYYY-mm-dd hh:mm:ss” to “dd-mm-YYYY hh:mm:ss” using javascript? 如何在不使用正则表达式的Javascript中将dd-MM-yyyy HH:mm:ss转换为纪元时间? - How to convert dd-MM-yyyy HH:mm:ss to epoch time in Javascript without regex? 如何使用moment.js将秒传输到时间格式(YYYY:MM:DD HH:mm:ss) - How to transfer seconds to Time format (YYYY:MM:DD HH:mm:ss) using moment.js 如何使用moment.js仅获取日期(DD-MM-YYYY)格式而不是日期时间? - How to get only date (DD-MM-YYYY) format not time with date using moment.js? 在 moment.js 中将 DD-MM-YYYY 格式转换为 MM-DD-YYYY - Convert DD-MM-YYYY format to MM-DD-YYYY in moment.js 使用moment.js(时区)时以(“ YYYY-MM-DD HH:mm”)格式返回日期和时间 - Return date and time in (“YYYY-MM-DD HH:mm”) format when using moment.js (timezone) 在moment.js中将“ yyyy-mm-ddThh:mm-offset”格式转换为“ yyyy-mm-dd HH:mm” - Convert 'yyyy-mm-ddThh:mm-offset' format to 'yyyy-mm-dd HH:mm' in moment.js 带有moment.js的dd-MM-yyyy日期格式 - dd-MM-yyyy date format with moment.js Javascript:如何将纪元转换为 dd-mm-yyyy - Javascript: how to convert epoch to dd-mm-yyyy Moment.js如何将MM / dd / yyyy HH:mm:ss.fff转换为往返日期/时间模式? - Moment.js How to convert MM/dd/yyyy HH:mm:ss.fff to round-trip date/time pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM