get current date with 'yyyy-MM-dd' format in Angular 4

前端 未结 10 943
孤城傲影
孤城傲影 2020-12-23 20:35

How i can get the current date with a specific format \'yyyy-MM-dd\', for today by example i with that the result be: \'2018-07-12\', with using just the command

<         


        
相关标签:
10条回答
  • 2020-12-23 20:51

    If you only want to display then you can use date pipe in HTML like this :

    Just Date:

    your date | date: 'dd MMM yyyy'
    

    Date With Time:

    your date | date: 'dd MMM yyyy hh:mm a'
    

    And if you want to take input only date from new Date() then :

    You can use angular formatDate() in ts file like this -

    currentDate = new Date();
    
    const cValue = formatDate(currentDate, 'yyyy-MM-dd', 'en-US');
    
    0 讨论(0)
  • 2020-12-23 20:53

    You can use date:'yyyy-MM-dd' pipe

    curDate=new Date();
    
    <p>{{curDate | date:'yyyy-MM-dd'}}</p>
    
    0 讨论(0)
  • 2020-12-23 20:55

    You can use DatePipe for formatting Date in Angular.

    In ts if you want to format date then you can inject DatePipe as Service in constructor like this

    import { DatePipe } from '@angular/common';
    
    @Component({
        templateUrl: './name.component.html',
        styleUrls: ['./name.component.scss'],
        providers: [DatePipe]
    })
    
    myDate = new Date();
    constructor(private datePipe: DatePipe){
        this.myDate = this.datePipe.transform(this.myDate, 'yyyy-MM-dd');
    }
    

    And if you want to format in html file, 'Shortdate' will return date of type MM/DD/YY

    {{myDate | date: 'shortDate' }}
    

    As of Angular 6, this also works,

    import {formatDate} from '@angular/common';
    
    formatDate(new Date(), 'yyyy/MM/dd', 'en');
    
    0 讨论(0)
  • 2020-12-23 20:58

    can use format attribute in HTML.

    0 讨论(0)
提交回复
热议问题