render Time in DD"/>
How to render Time in DD
我正在尝试在车把页面中以 DD-MM-YYYY 格式呈现当前日期。我在我的模型中使用了 moment 来获得客户的时间。我使用 Paper.findOne() 方法完美地返回了日期。但是当我使用 Paper.findAll() 和 Paper.findAndCountAll() 方法时,它以 YYYY-MM-DD 格式返回日期。甚至日期也以 YYYY-MM-DD 格式存储在数据库中。除此之外,我想将我的时间呈现为 AM/PM 格式。为此,我使用了 Sequelize 的时间数据类型。
这是我的模特
const Paper = db.define('paper', {
title: {
type: Sequelize.STRING
},
abstract: {
type: Sequelize.TEXT('long')
},
contributor: {
type: Sequelize.TEXT,
get() {
return JSON.parse(this.getDataValue("contributor"))
},
set(val) {
return this.setDataValue("contributor", JSON.stringify(val));
},
},
paper_file:
{
type: Sequelize.STRING
},
postingDate:{
type: Sequelize.DATEONLY,
get() {
return moment(this.getDataValue("postingDate")).format('DD-MM-YYYY');
}
},
postingTime:{
type:Sequelize.TIME
}
},
{
timestamps: false
}
);
在数据库中发布日期和时间时,我按以下方式修改
let postingDate = new Date().toLocaleDateString();
let postingTime = new Date().toLocaleTimeString();
这是我的 Sequelize 查询
Paper.findAll({
limit: 3,
order: [
['postingDate', 'DESC'],
['postingTime', 'DESC']
],
include:[{
model: PaperTopic,
},{
model:Category
}]
}) .then(paper=>{
res.render('home/index', {
layout: 'index-layout',
name: req.username,
email: req.email,
role: req.role,
paper,
})
})
这是我对 Paper.findAll() 的 hbs
{{#each paper}}
<div class="col-lg-12 col-12">
<div class="job-thumb d-flex">
{{!-- <div class="job-image-wrap bg-white shadow-lg">
<img src="/index_asset/images/logos/google.png" class="job-image img-fluid" alt="">
</div> --}}
<div class="job-body d-flex flex-wrap flex-auto align-items-center ms-4">
<div class="mb-3">
<h4 class="job-title mb-lg-0">
<a href="/paper/{{this.dataValues.title}}" class="job-title-link">{{this.dataValues.title}}</a>
</h4>
<p class="job-author mb-lg-0">
{{#each contributor}}
{{this}} {{#calculation}},{{/calculation}}
{{/each}}
</p>
<div class="d-flex flex-wrap align-items-center">
{{!-- <p class="job-date mb-0">
<i class="custom-icon bi bi-pen-fill me-1"></i>
{{#each contributor}}
{{this}} {{#calculation}},{{/calculation}}
{{/each}}
</p> --}}
<p class="job-location mb-0">
<i class="custom-icon bi-calendar me-1"></i>
{{this.dataValues.postingDate}}
</p>
<p class="job-date mb-0">
<i class="custom-icon bi-clock me-1"></i>
{{this.dataValues.postingTime}}
</p>
{{!-- <p class="job-price mb-0">
<i class="custom-icon bi-cash me-1"></i>
$20k
</p> --}}
<div class="d-flex">
<p class="mb-0">
<a href="job-listings.html" class="badge badge-level">{{this.dataValues.category.category}}</a>
</p>
<p class="mb-0">
<a href="job-listings.html" class="badge">{{this.dataValues.papertopic.paper_topic}}</a>
</p>
</div>
</div>
</div>
<div class="job-section-btn-wrap">
<a href="/papers/{{this.dataValues.title}}" class="custom-btn btn">Read More</a>
</div>
</div>
</div>
</div>
{{/each}}
这是我使用 .findAndCountAll() 方法的另一个查询
Paper.findAndCountAll({
limit: perPage,
offset: (perPage*page) - perPage,
order: [
['postingDate', 'DESC'],
['postingTime', 'DESC']
],
include:[{
model: PaperTopic,
//required: true
},{
model:Category
}]
}).then(paper=>{
res.render('papers/all-recent-papers', {
layout: 'index-layout',
paper: paper.rows,
total : paper.count,
category,
current: parseInt(page),
pages: Math.ceil(paper.count/perPage),
})
})
这是我的 hbs for .findAndCountAll()
{{#each paper}}
<div class="col-lg-12 col-12">
<div class="job-thumb d-flex">
<div class="job-body d-flex flex-wrap flex-auto align-items-center ms-4">
<div class="mb-3">
<h4 class="job-title mb-lg-0">
<a href="/paper/{{this.dataValues.title}}" class="job-title-link">{{this.dataValues.title}}</a>
</h4>
<p class="job-author mb-lg-0">
{{#each contributor}}
{{this}} {{#calculation}},{{/calculation}}
{{/each}}
</p>
<div class="d-flex flex-wrap align-items-center">
{{!-- <p class="job-date mb-0">
<i class="custom-icon bi bi-pen-fill me-1"></i>
{{#each contributor}}
{{this}} {{#calculation}},{{/calculation}}
{{/each}}
</p>
--}}
<p class="job-location mb-0">
<i class="custom-icon bi-calendar me-1"></i>
{{this.dataValues.postingDate}}
</p>
<p class="job-date mb-0">
<i class="custom-icon bi-clock me-1"></i>
{{this.dataValues.postingTime}}
</p>
{{!-- <p class="job-price mb-0">
<i class="custom-icon bi-cash me-1"></i>
$20k
</p>
--}}
<div class="d-flex">
<p class="mb-0">
<a href="job-listings.html" class="badge badge-level">{{this.dataValues.category.category}}</a>
</p>
<p class="mb-0">
<a href="job-listings.html" class="badge">{{this.dataValues.papertopic.paper_topic}}</a>
</p>
</div>
</div>
</div>
<div class="job-section-btn-wrap">
<a href="/papers/{{this.dataValues.title}}" class="custom-btn btn">Read More</a>
</div>
</div>
</div>
</div>
{{/each}
回答如下:
更多推荐
How to render Time in DD
发布评论