vue - 比较两个日期大小、比较同一天两个时间大小(判断两个日期时间的大小)JS 解决方法

介绍

vue项目中,可能需要对比两个日期时间的大小,如下代码所示:

2018-8-12 12:30   |   2018-8-12 02:30

它们谁大?

比较两个日期大小

方法一:

//比较两个日期大小。格式:2018-8-12 12:30
const compareDate = (d1, d2) => {
    
    
  return ((new Date(d1.replace(/\-/g, "\/"))) > (new Date(d2.replace(/\-/g, "\/"))));
}

方法二:

//比较两个日期大小。格式:2018-8-12 12:30
const compareDate2 = (d1, d2) => {
    
    
  let date1 = new Date(Date.parse(d1))
  let date2 = new Date(Date.parse(d2))
  return date1 > date2
}

比较 “同一天” 两个时间大小

//比较同一天的两个时间大小, 是否 t1 > t2。如 11:30 和 10:00, 返回true
const compareTimeInSameDay = (t1, t2) => {
    
    
  let d = new Date()
  let ft1 = d.setHours(t1.split(":")[0], t1.split(":")[1])
  let ft2 = d.setHours(t2.split(":")[0], t2.split(":")[1])
  return ft1 > ft2
}

猜你喜欢

转载自blog.csdn.net/weixin_50545213/article/details/128909487