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

前端 未结 10 942
孤城傲影
孤城傲影 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:35

    Try this:

       import * as moment from 'moment';
    
         ngOnInit() {
         this.date = moment().format("YYYY Do MMM");
                    }
    
    0 讨论(0)
  • 2020-12-23 20:40

    In Controller ,

     var DateObj = new Date();
    
     $scope.YourParam = DateObj.getFullYear() + '-' + ('0' + (DateObj.getMonth() + 1)).slice(-2) + '-' + ('0' + DateObj.getDate()).slice(-2);
    
    0 讨论(0)
  • 2020-12-23 20:43

    you can try doing this.


    component.ts

    currentDate = new Date();
    

    component.html

    {{currentDate | date:'yyyy-MM-dd'}}
    
    0 讨论(0)
  • 2020-12-23 20:44

    app.component.html

    <div>
         <h5 style="color:#ffffff;">{{myDate | date:'fullDate'}}</h5>
    </div>
    

    app.component.ts

    export class AppComponent implements OnInit {
    myDate = Date.now();    //date 
    
    0 讨论(0)
  • 2020-12-23 20:44

    Use the following:

    new Date().toLocaleDateString();
    

    Then as per your requirement just change locale for your application:

    import { registerLocaleData } from '@angular/common';
    import localeFr from '@angular/common/locales/fr';
    registerLocaleData(localeFr);
    
    //Provide the locale in any Module or Component by the LOCALE_ID token.
    providers: [
     {provide: LOCALE_ID, useValue: 'fr'}
    ]
    
    0 讨论(0)
  • 2020-12-23 20:46

    Here is the example:

    function MethodName($scope)
    {
        $scope.date = new Date();
    }
    

    You can change the format in view here we have a code

    <div ng-app ng-controller="MethodName">
        My current date is {{date | date:'yyyy-MM-dd'}} . 
    </div>
    

    I hope it helps.

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