将UNIX时间戳转换为月/日/年格式的函数

时间:2014-10-23 17:36:19

标签: javascript node.js date unix express

我正在使用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>

1 个答案:

答案 0 :(得分:0)

我假设您的时间戳是以数字形式出现的,而不是日期。请尝试console.log(typeof blogpost.date)仔细检查。如果是这样,请尝试:

blogpost.date = new Date(req.body.date);