Node.js 将ISODate更改为字符串日期表达式
如何更改数组映射上的“created_at”属性的值 这是我的目标Node.js 将ISODate更改为字符串日期表达式,node.js,express,isodate,Node.js,Express,Isodate,如何更改数组映射上的“created_at”属性的值 这是我的目标 { _id: 59661cba54481612500d3043, author_id: 595e51e0f14cff12e896ead7, updated_at: 2017-07-12T13:07:52.913Z, created_at: 2017-12-06T17:00:00.000Z, trash: false, tag: [ 595e51e0f14cff12e896ead9, 595e51e0f14cf
{ _id: 59661cba54481612500d3043,
author_id: 595e51e0f14cff12e896ead7,
updated_at: 2017-07-12T13:07:52.913Z,
created_at: 2017-12-06T17:00:00.000Z,
trash: false,
tag: [ 595e51e0f14cff12e896ead9, 595e51e0f14cff12e896ead3 ],
category: [ 595e51e0f14cff12e896ead9, 595e51e0f14cff12e896ead3 ],
title: 'test2' }
我想将在创建的价值更改为以下简单日期=“13-07-2017”
这是我的密码
function article(req, res) {
postArticle.find({}, function(err, articles) {
articles.map(function(article) {
var dateCreate = new Date(article.created_at);
var newDate = dateCreate.getDate()+'-' +(dateCreate.getMonth()+1)+'-' +dateCreate.getFullYear();
article.created_at = newDate;
console.log(dateCreate);
console.log(newDate)
console.log(article)
});
// res.render('admin/article/index', {title: 'Article Posts', posts: article})
// res.json({title: 'article', posts: article})
// console.log(article)
})}
但是我的代码不适用于更改:(我在项目中编写了一个方法来完成此操作,您可以使用此代码更改日期的格式
function article(req, res) {
postArticle.find({}, function(err, articles) {
articles.map(function(article) {
var dateCreate = formatDate(article.created_at);
var newDate = formatDate(new Date());
article.created_at = newDate;
console.log(dateCreate);
console.log(newDate)
console.log(article)
});
// res.render('admin/article/index', {title: 'Article Posts', posts: article})
// res.json({title: 'article', posts: article})
// console.log(article)
})}
格式化日期的方法:
var formatDate = function (date) {
var myDate = new Date(date);
var y = myDate.getFullYear(),
m = myDate.getMonth() + 1, // january is month 0 in javascript
d = myDate.getDate();
// Get the DD-MM-YYYY format
return formatDigit(d) + "-" + formatDigit(m) + "-" + y;
}
此方法将以两位数返回月或日。例如,ifformatDigit(4)//04
/**
* Format a month or day in two digit.
*/
var formatDigit = function (val) {
var str = val.toString();
return (str.length < 2) ? "0" + str : str
};
/**
*将月份或日期格式化为两位数。
*/
var formatDigit=函数(val){
var str=val.toString();
返回(str.length<2)?“0”+str:str
};
希望它能为您工作。我在我的项目中编写了一个方法来实现这一点,您可以使用此代码更改日期的格式
function article(req, res) {
postArticle.find({}, function(err, articles) {
articles.map(function(article) {
var dateCreate = formatDate(article.created_at);
var newDate = formatDate(new Date());
article.created_at = newDate;
console.log(dateCreate);
console.log(newDate)
console.log(article)
});
// res.render('admin/article/index', {title: 'Article Posts', posts: article})
// res.json({title: 'article', posts: article})
// console.log(article)
})}
格式化日期的方法:
var formatDate = function (date) {
var myDate = new Date(date);
var y = myDate.getFullYear(),
m = myDate.getMonth() + 1, // january is month 0 in javascript
d = myDate.getDate();
// Get the DD-MM-YYYY format
return formatDigit(d) + "-" + formatDigit(m) + "-" + y;
}
此方法将以两位数返回月或日。例如,ifformatDigit(4)//04
/**
* Format a month or day in two digit.
*/
var formatDigit = function (val) {
var str = val.toString();
return (str.length < 2) ? "0" + str : str
};
/**
*将月份或日期格式化为两位数。
*/
var formatDigit=函数(val){
var str=val.toString();
返回(str.length<2)?“0”+str:str
};
希望它对你有用