【问题标题】:How to get only time from 1970-01-01T09:30:00.000Z in Angular 2如何在 Angular 2 中仅从 1970-01-01T09:30:00.000Z 获取时间
【发布时间】:2019-04-25 10:08:36
【问题描述】:

我只想从 1970-01-01T09:30:00.000Z 获得时间。比如 9.30 或 16:00。如何做到这一点?

【问题讨论】:

标签: javascript angular date datetime time


【解决方案1】:

您可以使用 JavaScript 的 Date 类。这里有一个简短的例子:

const d = new Date('1970-01-01T09:30:00.000Z') // Parses a ISO 8601 Date
console.log(d.getHours()); // gets the hours in the timezone of the browser.
console.log(d.getUTCHours()); // gets the hours in UTC timezone.
console.log(d.getMinutes()); // gets the minutes in the timezone of the browser.
console.log(d.getUTCMinutes()); // gets the minutes in UTC timezone.
console.log(d.getHours() + ':' + d.getMinutes());
console.log(d.getUTCHours() + ':' + d.getUTCMinutes());

【讨论】:

    【解决方案2】:

    我希望这对将来的任何人都有帮助。 我想你可以像下面这样以角度使用日期管道

    {{ 2019-06-26T19:31:00.000Z | date:"dd/MM/yyyy HH:mm:ss"}}
    

    它会给出以下o/p 27/06/2019 01:01:00

    仅限时间使用以下

    {{ 2019-06-26T19:31:00.000Z | date:"HH:mm"}}
    

    它会给出以下o/p 01:01

    【讨论】:

      【解决方案3】:

      MomentJS 是一个很棒的 JS 项目时间管理库,Check it out !

      【讨论】:

      • 在commet中添加链接
      【解决方案4】:

      你可以像这样使用momentjs

       let YourDate = '1970-01-01T09:30:00.000Z'
       let time= moment(YourDate).format("hh:mm a")
      

      以下是一个有助于帮助的 stackbliz 网址:

      https://stackblitz.com/edit/angular4-momentjs-format-datetime-5etqib?file=app/app.component.ts

      【讨论】:

        【解决方案5】:

        你可以使用:

        date = new Date('1970-01-01T09:30:00.000Z');
        
        // and after this target by:
        date.getFullYear()
        date.getMonth()
        date.getDate();
        
        

        和其他需要的参数。

        【讨论】:

          猜你喜欢
          • 2021-10-12
          • 2021-05-19
          • 1970-01-01
          • 1970-01-01
          • 2012-09-26
          • 1970-01-01
          • 2015-05-21
          • 2018-08-26
          • 1970-01-01
          相关资源
          最近更新 更多