简体   繁体   English

在Angular中将日期格式从yyyy / MM / dd更改为MM / dd / yyyy

[英]Change date format from yyyy/MM/dd to MM/dd/yyyy in Angular

1st attempt , did this: 第一次尝试 ,这样做:

{{ leads.pgDate | date:'MM/dd/yyyy' }}

I've also tried: 我也尝试过:

" | date:"MM/dd/yyyy": 'UTC' "

2nd attempt , went to leadsCtr.js and found 第二次尝试 ,去了leadsCtr.js并找到了

$scope.leadsGridOptions = { 
  columnDefs: [ 
    {
      field: 'expected',
      displayName: 'Expected Close Date', width: 150, type: 'date',
cellFilter: 'date:\'MM/dd/yyyy\'' 
    }
  ]
}

Added 'type:''date'' and changed cellFilter from \\'sort\\' 添加'type:''date''并从\\'sort\\'更改了cellFilter

Observations: 观察:

displayName: 'Expected Close Date' BUT the date header in HTML is 'Date of Purchase' - that's why the 2nd attempt didn't work. displayName:'预期关闭日期' HTML中的日期标题是'购买日期' - 这就是第二次尝试不起作用的原因。 Also, cellFilter is being overridden, that addition didn't alter anything either. 此外,正在覆盖cellFilter ,该添加也没有改变任何东西。

{{leads.pgDate.toString()}} 

adding toString didn't change anything- Maybe date is a string?! 添加toString没有改变任何东西 - 也许date是一个字符串?!

New Problem: 新问题:

Cannot find the object that ng-repeat is using to populate the fields to see if the date really is a string and I can parse it. 无法找到ng-repeat用于填充字段的对象,以查看日期是否真的是一个字符串,我可以解析它。

Questions: 问题:

If date is coming in as String, will the angular filter not work? 如果日期以字符串形式出现,则角度过滤器不起作用吗? Is there anyway to override {{inside the HTML}} ? 无论如何要覆盖{{inside the HTML}}

Answer: 回答:

Used Jimbrooism's suggestion. 使用了Jimbrooism的建议。 The wrapper is converting the value back into a date format and the filter works. 包装器将值转换回日期格式,过滤器可以正常工作。

Try This {{convertDate(leads.pgDate) | 试试这个{{convertDate(leads.pgDate)| date:'dd/MMM/yyyy'}} 日期: 'DD / MMM / YYYY'}}

//JS // JS

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

if you getting date as a string yyyy-mm-dd then convert this into java script date obj. 如果您将日期作为字符串yyyy-mm-dd,则将其转换为java脚本日期obj。 After convert it into date obj it is easy to convert into any format. 将其转换为日期obj后,很容易转换为任何格式。 for mm-dd-yyyy format see this link How to get current formatted date dd/mm/yyyy in Javascript and append it to an input 对于mm-dd-yyyy格式,请参阅此链接如何在Javascript中获取当前格式化的日期dd / mm / yyyy并将其附加到输入

I suggest to use moment and angular moment for date related stuff. 我建议使用时刻角度时刻来表示日期相关的东西。

In the controller: 在控制器中:

$scope.date = moment(<date>, 'YYYY/MM/DD');

In the view: 在视图中:

<p data-ng-bind="date | amDateFormat : 'MM/DD/YYYY'"></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM