我有一个功能,可以显示用户输入的实时日期,现在当用户输入输入时,我在前端显示[日期]:
28.10.2018 10:09
我希望更改日期(如果是过去几天、过去一周、过去一年等)
因此,如果昨天输入了输入,我想显示如下内容:
1d
意思是一天前,一年也是如此(1y
) , 一周 (1w
) etc.
这是我到目前为止所尝试过的:
这是获取日期及其输入文本的函数
this.activeRouter.params.subscribe((params) => {
let id = params['id'];
this.userService.getComments(id)
.pipe(
map(data => data.sort((a, b) => new Date(b.localTime).getTime() - new Date(a.localTime).getTime()))
)
.subscribe(data => this.comments = data);
});
他这里是将文本输入和日期添加到服务器的功能
addComments(task_id) {
const formData = this.addForm.value;
formData.task_id = task_id;
this.userService.addComments(formData)
.subscribe(data => {
this.comments.push(this.addForm.value);
this.addForm.reset();
});
const date = new Date();
const d = date.getUTCDate();
const day = (d < 10) ? '0' + d : d;
const m = date.getUTCMonth() + 1;
const month = (m < 10) ? '0' + m : m;
const year = date.getUTCFullYear();
const h = date.getUTCHours();
const hour = (h < 10) ? '0' + h : h;
const mi = date.getUTCMinutes();
const minute = (mi < 10) ? '0' + mi : mi;
const sc = date.getUTCSeconds();
const second = (sc < 10) ? '0' + sc : sc;
const loctime = `${year}-${month}-${day}T${hour}`;
this. addForm.get('localTime').setValue(loctime);
}
这里是展示给前端的html
HTML:
<div class="comments_details">
<h1>Mike Ross</h1>
<span class="days">{{comment.localTime | date:'dd.MM.yyyy H:mm'}}</span>
</div>
这是用于抓取数据并将数据添加到服务器的服务函数
addComments(comments: Comment) {
comments.localTime = new Date();
return this.http.post(this.commentsUrl, comments);
}
getComments(id: number) {
return this.http.get<Comment[]>(this.commentsUrl);
}
我需要在代码中更改哪些内容才能获得我想要的格式?
我认为你不应该在角度中使用力矩。它不是 tree shakable 的,并且会让你的包变得巨大。您应该看一下 date-fns 或 date.js。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)