【问题标题】:Get difference between dates and format with 'DD/MM/YYYY HH:MM:SS'使用 'DD/MM/YYYY HH:MM:SS' 获取日期和格式之间的差异
【发布时间】:2020-04-16 14:28:17
【问题描述】:

如何将date.now() 中的毫秒转换为DD/MM/YYYY HH:MM:SS 格式或将带有日期的字符串转换为毫秒。

我还有一个返回以下内容的字符串:

appointment: "21/11/2019 18:00:30"

我需要得到 date.nowappointment 日期之间的时间差

【问题讨论】:

  • 请从 Angular 5 更新到更新的版本。不再支持 Angular 5
  • @ShamPooSham 那是不可能的,也不取决于我呵呵

标签: javascript angular typescript angular7 angular8


【解决方案1】:

您需要将日期字符串转换为像"2019-11-21T18:00:30"; 这样的有效格式,然后使用.getTime() 获取差异

试试这样:

let difference = Math.abs(new Date().getTime() - new Date(this.appointment).getTime());

var seconds = difference / 1000;
var minutes = difference / 1000 / 60;
var hours = minutes / 60;

console.log("Difference in Hours",hours)
console.log("Difference in minutes",minutes)
console.log("Difference in seconds",seconds)

Working Demo

【讨论】:

  • ? 干得好,但是你能给出关于日期格式的更多解释吗?这会很有帮助
  • 感谢 @Adrita 更新答案,现在它更有帮助
【解决方案2】:

这里是working Example

var d = new Date,
dformat = [d.getDate(),
          d.getMonth()+1 ,
           d.getFullYear()].join('/')+' '+
          [d.getHours(),
           d.getMinutes(),
           d.getSeconds()].join(':');
           console.log(dformat)

输出

26/12/2019 15:48:17

【讨论】:

    【解决方案3】:

    要转换日期,您可以使用 DatePipe

    let datePipe = new DatePipe();
    datePipe.transform(date, 'dd/MM/yyyy hh:mm:ss');
    

    要了解 Date.now() 和日期之间的区别,您可以查看此 answer,或者您可以像这样使用 moment

    import * as moment from 'moment';
    
    // ...
    
    const difference = moment(moment(),"DD/MM/YYYY HH:mm:ss").diff(moment(appointment,"DD/MM/YYYY HH:mm:ss"))
    

    你需要像这样安装moment

    npm install --save moment
    

    【讨论】:

    • 注入 moment.js 只是为了得到 datedif 不被认为是一个意志解决方案,但我想你正在使用它是一个很棒的库 ?
    • @veben 日期是什么?现在日期?
    • @PLATANIUM,这将是您要转换的日期
    【解决方案4】:

    小时差:

    const dateAppointment = new Date('1/11/2019 18:00:30');
    const currentDate = new Date();
    console.log(`currentDate is `, currentDate );
    const diffTime = Math.abs(currentDate - dateAppointment);
    const diffHours = Math.ceil(diffTime / (1000 * 60 * 60)); 
    console.log(`diffHours is`, diffHours );
    

    一个例子:

    const dateAppointment = new Date('1/11/2019 18:00:30');
    const currentDate = new Date();
    console.log(`currentDate is `, currentDate );
    const diffTime = Math.abs(currentDate - dateAppointment);
    const diffHours = Math.ceil(diffTime / (1000 * 60 * 60)); 
    console.log(`diffHours is`, diffHours );

    更新:

    HTML:

    <p>currentDate is {{ currentDate | date : 'dd.MM.y, HH:mm:ss' }} </p>
    <p>dateAppointment is {{ dateAppointment | date : 'dd.MM.y, HH:mm:ss' }} </p>
    <p>differenceHour is {{ differenceHour }} </p>
    

    打字稿:

     name = 'Angular';
     currentDate = new Date();
     dateAppointment = new Date('1/11/2019 18:00:30');
     differenceHour = 0;
    
     constructor(){    
         const diffTime = Math.abs(this.currentDate - this.dateAppointment);
         this.differenceHour = Math.ceil(diffTime / (1000 * 60 * 60)); 
     }
    

    An stackblitz example can be seen here

    【讨论】:

    • 我收到此错误:算术运算的左侧必须是 'any'、'number'、'bigint' 或枚举类型。ts(2362) 具有以下行代码: const diffTime = Math.abs(currentDate - dateAppointment);
    【解决方案5】:

    有一种方法可以在没有第三方的情况下做到这一点

    let date = new Date(Date.now());
    date = `${date.getDate()}:${date.getMonth() + 1}:${date.getFullYear()}:${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
    

    如果你使用 Angular,还有一种使用 Angular 的方法,你可以使用带有变换方法的“DatePipe”,就像这样:

    datePipe.transform(date, 'dd/MMM/yyyy hh:mm:ss');
    

    要获得不同的日期,只有一种方法

    const currentDate = new Date('26/12/2019');
    const appoinemnt = new Date('15/1/2020');
    const diffTime = Math.abs(appoinemnt - currentDate );
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
    console.log(diffDays);
    

    【讨论】:

    • ?干得好?但你需要提到你已经注入了日期管道
    猜你喜欢
    • 2023-02-24
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 2020-03-07
    • 2023-03-03
    相关资源
    最近更新 更多