javascript 日期格式 AngularJS 只显示日期和月份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25845202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Date formatting AngularJS to display only the day and month
提问by divakar
I am completed newbie to Angular. So I have date value for created_at. which is
我是 Angular 的新手。所以我有 created_at 的日期值。这是
2008-04-25 00:00:00
I want to get only the day similarly month in English. I have tried the following to make sample to get Year alone. But the filter not seems to be working. What am I doing wrong?
我只想用英语得到同样月份的那一天。我尝试了以下方法来制作样本以单独获得 Year。但过滤器似乎不起作用。我究竟做错了什么?
<h2>Recent Study</h2>
<ul ng-repeat="x in names">
<li>
<div class="date">
<span class="day">{{ x.created_at | date : 'yyyy' }}</span>
<span class="month">Jul</span>
</div>
<p><a href="#">{{ x.study_description }}</a></p>
</li>
</ul>
回答by Nitsan Baleli
You need to convert the date to seconds, and use a custome filter:
您需要将日期转换为秒,并使用客户过滤器:
app.filter('toSec', function($filter) {
return function(input) {
var result = new Date(input).getTime();
return result || '';
};
});
and use it in your HTML like this:
并在您的 HTML 中使用它,如下所示:
{{ '2008-04-25 00:00:00' | toSec | date:'EEEE'}}
result:
结果:
Friday
Angular gives you the option to create a Custome Filter
Angular 为您提供了创建自定义过滤器的选项
Here's a working examplethat achieves what you need using a custome filter.
这是一个使用客户过滤器实现您需要的工作示例。
回答by Rusty
created_at's formatting isn't compatible.
created_at 的格式不兼容。
Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.
格式化为日期对象、毫秒(字符串或数字)或各种 ISO 8601 日期时间字符串格式的日期(例如 yyyy-MM-ddTHH:mm:ss.sssZ 及其较短的版本,如 yyyy-MM-ddTHH:mmZ、yyyy-MM -dd 或 yyyyMMddTHHmmssZ)。如果字符串输入中未指定时区,则时间被视为本地时区。
Cited from https://docs.angularjs.org/api/ng/filter/date
引自https://docs.angularjs.org/api/ng/filter/date
for example "2008-04-25T00:00:00" would work.
例如“2008-04-25T00:00:00”就可以了。
回答by user3812457
I hope your getting the input as time stamp ,in your code just change the filter to get the only day
我希望您将输入作为时间戳记,在您的代码中只需更改过滤器即可获得唯一的一天
<h2>Recent Study</h2>
<ul ng-repeat="x in names">
<li>
<div class="date">
<!-- getting only day -->
<span class="day">{{ x.created_at | date:'dd '}}</span>
<!-- getting month name and day only -->
<span class="month">{{ x.created_at | date:'MMM-dd '}}</span>
</div>
<p><a href="#">{{ x.study_description }}</a></p>
</li>
</ul>
here date:'dd '
gives Day in month, padded (01-31) and
这里 date:'dd '
给出月份中的日期,填充(01-31)和
date:'MMM-dd ' gives Month in year (Jan-Dec) and above day in month.
回答by divakar
I have found the answer. The problem is simple. We should convert the mysql date time to javascript date time then only you can format it in javascript.
我找到了答案。问题很简单。我们应该将 mysql 日期时间转换为 javascript 日期时间,然后只有您可以在 javascript 中对其进行格式化。
so my format is:2008-04-25 00:00:00
所以我的格式是:2008-04-25 00:00:00
app.filter('format', function () {
return function (item) {
var t = item.split(/[- :]/);
var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
var time=d.getTime();
return time;
};
});
I have converted the mysql to javascript and then to millisecond then applied my filter its working.This may be not efficient. Its working in great manner. Hope this helps someone.
我已将 mysql 转换为 javascript,然后转换为毫秒,然后应用我的过滤器使其正常工作。这可能效率不高。它的工作方式很好。希望这可以帮助某人。
so my html:
所以我的 html:
<li ng-repeat="x in names">
<div class="date">
<span class="day">{{ x.created_at | format | date:'d'}}</span>
<span class="month">{{ x.created_at | format | date:'MMM'}}</span>
</div>
<p><a href="#">{{ x.study_description | makeUppercase}}</a></p>
</li>
Its working fine. @Nishanth answer is giving me undefined. I have upvoted it since its seems working fine for some people. The date format support vary on browser tools too.
它的工作正常。@Nishanth 的回答给了我未定义的。我已经赞成它,因为它对某些人来说似乎工作正常。日期格式支持也因浏览器工具而异。