函数将UNIX时间戳转换为月/日期/年格式
Function to convert UNIX Timestamp to month/date/year Format
我使用的是ExpressJS,当我试图获取日期值时,我希望将UNIX格式的日期转换为mm/dd/yyyy格式。我试图创建一个函数,将我的数据转换为这种新格式,但我无法调用我的方法,因为blogpost.date
似乎是未定义的。我是否应该在路线的其他部分使用此转换函数?此外,我是否正确地使用了包含此转换后的时间戳的新变量?感谢您的帮助。
错误:
/Users/user/Desktop/Projects/node/blog/node_modules/mongoose/lib/utils.js:419
throw err;
^
TypeError: Cannot call method 'getMonth' of undefined
at dateDisplayed (/Users/user/Desktop/Projects/node/blog/app/routes.js:39:53)
at /Users/user/Desktop/Projects/node/blog/app/routes.js:42:25
at /Users/user/Desktop/Projects/node/blog/node_modules/mongoose-paginate/lib/mongoose-paginate.js:47:5
at /Users/user/Desktop/Projects/node/blog/node_modules/mongoose-paginate/node_modules/async/lib/async.js:592:17
at done (/Users/user/Desktop/Projects/node/blog/node_modules/mongoose-paginate/node_modules/async/lib/async.js:135:19)
at /Users/user/Desktop/Projects/node/blog/node_modules/mongoose-paginate/node_modules/async/lib/async.js:32:16
at /Users/user/Desktop/Projects/node/blog/node_modules/mongoose-paginate/node_modules/async/lib/async.js:589:21
at /Users/user/Desktop/Projects/node/blog/node_modules/mongoose-paginate/lib/mongoose-paginate.js:40:9
at /Users/user/Desktop/Projects/node/blog/node_modules/mongoose/lib/utils.js:414:16
at /Users/user/Desktop/Projects/node/blog/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection.js:594:7
模型(blogModel.js):
var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
var Schema = mongoose.Schema;
var BlogPostSchema = new Schema({
title: String,
blogUrl: String,
author: String,
tagline: String,
category: String,
content: String,
tags: { type: String, lowercase: true },
date: { type: Date, default: Date.now }
});
BlogPostSchema.plugin( mongoosePaginate );
var Blogpost = mongoose.model("Blogpost", BlogPostSchema);
module.exports = mongoose.model('Blogpost', BlogPostSchema);
路由(routes.js):
var express = require('express');
var router = express.Router();
var blogDB = require('../config/blogDB.js');
var Blogpost = require('./models/blogModel.js');
var paginate = require('express-paginate');
//index
router.use(paginate.middleware(10, 50));
router.route('/')
// START GET method
.get(function(req, res, next) {
Blogpost.paginate({}, req.query.page, req.query.limit, function(err, pageCount, blogpost, itemCount) {
if (err) return next(err)
if (err)
res.send(err);
blogpost.title = req.body.title; // get the blog title
blogpost.author = req.body.author; // get the author name
blogpost.tagline = req.body.tagline; // get tagline
blogpost.content = req.body.content; // get the blog content
blogpost.category = req.body.category; // get the category
blogpost.tags = req.body.tags; // get the tags
blogpost.date = req.body.date;
function dateDisplayed(date){
var dateCreated = (date.getMonth() + 1 + '/' + date.getDate() + '/' + date.getFullYear());
}
dateDisplayed(blogpost.date);
console.log(dateDisplayed);
res.format({
html: function() {
res.render('pages/index', {
blogpost: blogpost,
pageCount: pageCount,
itemCount: itemCount
})
},
json: function() {
res.json({
object: 'blogpost',
has_more: paginate.hasNextPages(req)(pageCount),
data: blogpost
})
}
}); // END res.format(html, json)
}); // END Blogpost.paginate
}); // END GET method
index.ejs:
<div class="blog-content">
<% blogpost.forEach(function(blogpost) { %>
<tr>
<td><h2><a href="#" class="blog-title"><%= blogpost.title %></a></h2></td>
<td><h3><%= blogpost.dateCreated %></h3></td>
<td><h3 class="blog-category"><%= blogpost.category %></h3></td>
<td><h3 class="blog-tagline"><i><%= blogpost.tagline %></i></h3></td>
<td><p><%=: blogpost.content | truncate:800 | append:'...' %></p></td>
<td><a href="/blog/<%= blogpost.blogUrl %>" class="blog-read-more">Read More</a></td>
</tr>
<% }); %>
</div>
我假设您的时间戳是一个数字,而不是日期。请尝试console.log(typeof blogpost.date)
对此进行双重检查。如果是,请尝试:
blogpost.date = new Date(req.body.date);
相关文章:
- Javascript日期格式类似于ISO,但本地
- NodeJS日期格式不起作用
- 将客户端特定的日期格式返回到服务器MVC4
- ui网格日期单元格过滤器,过滤日期格式导致显示错误的日期
- 将Javascript日期转换为ASP.NET日期格式
- 在达到状态之前在Redux Reducer中设置日期格式
- 如何从Angularjs中的新日期获取日期格式
- 如何使用jquery更改为所需的日期格式
- keith wood日期选择器日期格式和语言
- Dygraph更改日期格式
- 确定Twig(或JavaScript)中的国际日期格式,以便在其他地方使用
- javascript和jquery日期格式转换
- 用javascript将日期格式从d/m/yyyy转换为yyyy-mm-dd
- 在Datepicker中将日期格式更改为yyyy-mm-dd
- 在Karma Jasmine单元测试用例中检查日期格式
- 使用不同的日期格式和区域设置创建时刻
- 更改日期选择器上的日期格式
- 以日期计算的天数更改日期格式
- 带有onchange的日期格式
- 如何从区域设置获取日期格式