【问题标题】:get current date with 'yyyy-MM-dd' format in Angular 4在Angular 4中以'yyyy-MM-dd'格式获取当前日期
【发布时间】:2018-07-12 07:42:23
【问题描述】:

我如何获得具有特定格式“yyyy-MM-dd”的当前日期,例如我今天的结果是:“2018-07-12”,仅使用命令

myDate = new Date();

非常感谢

【问题讨论】:

标签: angular


【解决方案1】:

您可以使用 DatePipe 在 Angular 中格式化日期。

如果你想在 ts 中格式化日期,那么你可以像这样在构造函数中将 DatePipe 作为服务注入

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');
}

如果你想在 html 文件中格式化,'Shortdate' 将返回 MM/DD/YY 类型的日期

{{myDate | date: 'shortDate' }}

从 Angular 6 开始,这也有效,

import {formatDate} from '@angular/common';

formatDate(new Date(), 'yyyy/MM/dd', 'en');

【讨论】:

  • 我从 @angular/common 导入了 DatePipe 并应用了您的代码,但我收到一条错误消息,指出 DateType 类型上不存在格式?
  • 还需要在providers中添加DatePipe
  • 我这样做了,错误消息是: src/app/filter-test/filter-test.component.ts(40,33) 中的错误:错误 TS2339:属性“格式”不存在在类型“DatePipe”上。
  • this.datePipe 提出的唯一方法是变换?
  • 我用这个命令解决了这个问题: test: string;我的日期 = 新日期(); this.test = this.datePipe.transform(this.myDate, 'yyyy-MM-dd');它有效
【解决方案2】:

你可以试试这个。


component.ts

currentDate = new Date();

component.html

{{currentDate | date:'yyyy-MM-dd'}}

【讨论】:

  • 简单、轻松、干净
【解决方案3】:

你可以使用date:'yyyy-MM-dd'管道

curDate=new Date();

<p>{{curDate | date:'yyyy-MM-dd'}}</p>

【讨论】:

  • 我的类型脚本文件中需要该值
【解决方案4】:

如果你只想显示,那么你可以像这样在 HTML 中使用日期管道:

只是日期:

your date | date: 'dd MMM yyyy'

日期与时间:

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

如果您只想从 new Date() 中获取输入日期,那么:

你可以像这样在 ts 文件中使用 angular formatDate() -

currentDate = new Date();

const cValue = formatDate(currentDate, 'yyyy-MM-dd', 'en-US');

【讨论】:

    【解决方案5】:

    示例如下:

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

    您可以在此处更改格式,我们有代码

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

    希望对你有帮助。

    【讨论】:

    • 感谢您的回答,但我在 Angular 4 中,我希望在我的类型脚本文件中使用此值,而不是在 html 中
    【解决方案6】:

    在控制器中,

     var DateObj = new Date();
    
     $scope.YourParam = DateObj.getFullYear() + '-' + ('0' + (DateObj.getMonth() + 1)).slice(-2) + '-' + ('0' + DateObj.getDate()).slice(-2);
    

    【讨论】:

      【解决方案7】:

      试试这个:

         import * as moment from 'moment';
      
           ngOnInit() {
           this.date = moment().format("YYYY Do MMM");
                      }
      

      【讨论】:

        【解决方案8】:

        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 
        

        【讨论】:

        • 如果代码附有简短、清晰的解释,说明您的答案解决问题的原因,并且在以下情况下,答案更有可能对未来的人们有用(并获得支持)一个带有 8 个其他答案的老问题,解释为什么您的答案会添加其他答案中缺少的内容。
        【解决方案9】:

        要在 Angular 中格式化日期,我们可以使用 Angular Datepipe。

        例如,我们可以使用以下代码在我们的代码中获取格式化日期。

        import { DatePipe } from '@angular/common';
        
        @Component({
            selector: 'app-name-class',
            templateUrl: './name.component.html',
            styleUrls: ['./name.component.scss']
        })
        
        export class NameComponent implements OnInit {
          
          // define datepipe
        
          datePipe: DatePipe = new DatePipe('en-US');
          
          constructor(){}
        
          // method to get formatted date
        
          getformattedDate(){
            
            var date = new Date();
            var transformDate = this.datePipe.transform(date, 'yyyy-MM-dd');
            return transformDate;
        
          }
        }
        
        

        【讨论】:

          【解决方案10】:

          你可以使用简单的 var dt = new Date().toISOString().slice(0, 10);

          【讨论】:

          • 是的,没错,但我更喜欢 DatePipe。
          【解决方案11】:

          使用以下内容:

          new Date().toLocaleDateString();
          

          然后根据您的要求更改应用程序的语言环境:

          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'}
          ]
          

          【讨论】:

            【解决方案12】:

            可以在 HTML 中使用格式属性。

            【讨论】:

            • 作为答案,这确实需要更多细节才能真正回答问题。你指的是什么format 属性?你能提供一个文档链接吗?演示如何在此处应用它的代码示例?为什么这种方法可能比这个线程上接受的答案更受欢迎?
            猜你喜欢
            • 2011-12-06
            • 2013-10-04
            • 1970-01-01
            • 2011-06-29
            • 1970-01-01
            • 2011-09-09
            • 2016-01-02
            • 1970-01-01
            • 2012-12-09
            相关资源
            最近更新 更多