检查日期是否从开始日期起超过 24 小时。
Ex:
My date string **"27-05-2021 10:26:29 PM"**. I want to check from this start date exceeds or not. I'm using moment.js
答案 0 :(得分:2)
您可以使用 moment js format
和 diff
函数。
let a = moment("27-05-2021 10:26:29 PM", "DD-MM-YYYY hh:mm:ss A");
let b = moment();//todays date in local time zone
console.log(a.diff(b, "hours"));
答案 1 :(得分:0)
您可以使用差异和持续时间
var x = moment('27-05-2021 10:26:29 PM', ['DD-MM-YYYY HH:NN:SS']);
var y = new moment()
var duration = moment.duration(x.diff(y)).as('hours');
console.log(duration);
答案 2 :(得分:0)
首先,您需要解析字符串以获取有效日期。 如果直接使用moment(dateString),则结果为无效日期:
const dateString = '27-05-2021 10:26:29 PM';
const startDate = moment(dateString); // invalid date
所以:
// parse string using format string to get a valid date
const dateString = '27-05-2021 10:26:29 PM';
const startDate = moment(dateString, 'DD-MM-YYYY hh:mm:ss a');
// finally get the difference from other date in hours/minutes/years
const today = moment(); //to get todays date
const hoursDiff = today.diff(startDate, 'hours'); //diff in hours
const minutesDiff = today.diff(startDate, 'minutes'); //diff in minutes