Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用angular 2将当前日期转换为YYYY-MM-DD格式_Angular_Date_Typescript - Fatal编程技术网

如何使用angular 2将当前日期转换为YYYY-MM-DD格式

如何使用angular 2将当前日期转换为YYYY-MM-DD格式,angular,date,typescript,Angular,Date,Typescript,我使用此行获取当前日期 public current_date=new Date(); 我得到了这个结果: Wed Apr 26 2017 10:38:12 GMT+0100 (Afr. centrale Ouest) 如何将其转换为这种格式 YYYY-MM-DD 根据文件的示例 @Component({ selector: 'date-pipe', template: `<div> <p>Today is {{today | date}}</p

我使用此行获取当前日期

public current_date=new Date();
我得到了这个结果:

Wed Apr 26 2017 10:38:12 GMT+0100 (Afr. centrale Ouest)
如何将其转换为这种格式

YYYY-MM-DD

根据文件的示例

@Component({
  selector: 'date-pipe',
  template: `<div>
    <p>Today is {{today | date}}</p>
    <p>Or if you prefer, {{today | date:'fullDate'}}</p>
    <p>The time is {{today | date:'jmZ'}}</p>
  </div>`
})
export class DatePipeComponent {
  today: number = Date.now();
}
在组件中使用

@Injectable()
import { DatePipe } from '@angular/common';
class MyService {

  constructor(private datePipe: DatePipe) {}

  transformDate(date) {
    this.datePipe.transform(myDate, 'yyyy-MM-dd'); //whatever format you need. 
  }
}
在你的app.module.ts中

providers: [DatePipe,...] 

你现在要做的就是使用这项服务

试试下面的代码,它在angular 2中也能很好地工作

<span>{{current_date | date: 'yyyy-MM-dd'}}</span>
{{当前日期}日期:'yyyy-MM-dd'}

这是一种非常好的简洁方法,您也可以根据您的案例需要更改此功能:

结果:2017年11月3日

//get date now function
    getNowDate() {
    //return string
    var returnDate = "";
    //get datetime now
    var today = new Date();
    //split
    var dd = today.getDate();
    var mm = today.getMonth() + 1; //because January is 0! 
    var yyyy = today.getFullYear();
    //Interpolation date
    if (dd < 10) {
        returnDate += `0${dd}.`;
    } else {
        returnDate += `${dd}.`;
    }

    if (mm < 10) {
        returnDate += `0${mm}.`;
    } else {
        returnDate += `${mm}.`;
    }
    returnDate += yyyy;
    return returnDate;
}
//立即获取日期函数
getNowDate(){
//返回字符串
var returnDate=“”;
//现在就开始约会
var today=新日期();
//分裂
var dd=today.getDate();
var mm=today.getMonth()+1;//因为一月是0!
var yyyy=today.getFullYear();
//插值日期
如果(dd<10){
returnDate+=`0${dd}`;
}否则{
returnDate+=`${dd}`;
}
如果(毫米<10){
returnDate+=`0${mm}`;
}否则{
returnDate+=`${mm}`;
}
返回日期+=yyyy;
返回日期;
}
用于角度5

应用程序模块.ts

import {DatePipe} from '@angular/common';
.
.
.
providers: [DatePipe]
import { DatePipe } from '@angular/common';
.
.
constructor(private datePipe: DatePipe) {}

ngOnInit() {
   var date = new Date();
   console.log(this.datePipe.transform(date,"yyyy-MM-dd")); //output : 2018-02-13
}
demo.component.ts

import {DatePipe} from '@angular/common';
.
.
.
providers: [DatePipe]
import { DatePipe } from '@angular/common';
.
.
constructor(private datePipe: DatePipe) {}

ngOnInit() {
   var date = new Date();
   console.log(this.datePipe.transform(date,"yyyy-MM-dd")); //output : 2018-02-13
}

更多信息

添加模板并指定日期管道,您需要使用转义字符作为日期格式。您可以根据需要提供任何格式,如“MM yyyy dd”等

template: '{{ current_date | date: \'yyyy-MM-dd\' }}',

ES6/TypeScript中的解决方案:

设d=新日期;
log(d,[`d.getFullYear()}`、`0${d.getMonth()}`.substr(-2)、`0${d.getDate()}`.substr(-2)].join(“-”);

OP的预期输出在哪里?@RameshRajendran:updated实际上我需要它来在组件中进行未来的日期比较此
日期:“yyyy/MM/dd”
是使用角度设置日期格式的常用筛选方法,您可以创建一个常量来声明
“yyy/MM/dd”
的格式,并将该常量调用到您的组件请给我同样的例子,如何在格式化后将当前日期从模板传递到组件,只需使用
newdate(this.current\u date.replace(/-/g,“/”)或更多详细信息:我收到此错误:错误错误:InvalidPipeArgument:'缺少区域设置“de de”的区域设置数据。'管道“DatePipe”