【发布时间】:2019-04-25 10:08:36
【问题描述】:
我只想从 1970-01-01T09:30:00.000Z 获得时间。比如 9.30 或 16:00。如何做到这一点?
【问题讨论】:
-
'1970-01-01T09:30:00.000Z'.split('T')[1].split(':').slice(0,2).join(':')
标签: javascript angular date datetime time
我只想从 1970-01-01T09:30:00.000Z 获得时间。比如 9.30 或 16:00。如何做到这一点?
【问题讨论】:
'1970-01-01T09:30:00.000Z'.split('T')[1].split(':').slice(0,2).join(':')
标签: javascript angular date datetime time
您可以使用 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());
【讨论】:
我希望这对将来的任何人都有帮助。 我想你可以像下面这样以角度使用日期管道
{{ 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
【讨论】:
MomentJS 是一个很棒的 JS 项目时间管理库,Check it out !
【讨论】:
你可以像这样使用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
【讨论】:
你可以使用:
date = new Date('1970-01-01T09:30:00.000Z');
// and after this target by:
date.getFullYear()
date.getMonth()
date.getDate();
和其他需要的参数。
【讨论】: