在AngularJS中将毫秒转换为DD/MM/YYYY格式的日期

8

我编写了一个返回毫秒级时间戳的服务,但我想以 'DD/MM/YYYY' 的格式显示它,应该怎么做?


你需要格式化日期,例如你可以使用这个链接:http://blog.stevenlevithan.com/archives/date-time-format - pichsenmeister
6个回答

17

使用date过滤器,并指定格式,例如:

{{date_in_milliseconds | date:'dd/MM/yyyy'}}

5

var date = new Date(1324339200000);

date.toString("MMM dd"); // "Dec 20"

变量date被赋值为时间戳1324339200000对应的日期。使用toString方法并传入参数"MMM dd",可以将日期转换为字符串格式"Dec 20"。

3
  time: any;
  date: any;
  calculateDate() {
    let milliSeconds = new Date().getTime();
    let date = new Date(milliSeconds);
    this.time = date.toLocaleTimeString();
    this.date = date.toLocaleDateString();
    console.log('time', this.time, this.date);
  };

2

1
在Angular 2中:
var date = new Date(parseInt(1495777991000)); //datetime in IST
var time= date.toLocaleTimeString() //retrieve time Fri May 26 2017 11:23:11
console.log(time) //11:23:11

0

你也可以在 AngularJS 中使用管道。你可以在 HTML 文件中编写以下代码:

{{date|date:'日期格式'}}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接