Categories
javascript nodejs

Friendly dates with MongoDB and Moment.js

I’m working on a project where any new comment posted gets tagged with a timestamp in milliseconds (local time). My issue was when I display the comments but I want to compare the saved timestamp with the current time. I need to be able to still keep the saved timestamp and show a humanized version to the user.

Here is what I had to do. Works for my situation but perhaps there is a better solution. I used underscore.js. I iterated over the returned document and modified the timestamp property with the humanized version. Now instead of the user seeing something like 1123992032 they see “few minutes ago” depending on the current time.

app.get('/view-comments/:id', function(req, res){
	var paramId = req.params.id;
	MongoClient.connect(dbURL, function(err, db) {
		var col = db.collection('businesses');	
		col.findOne({_id: paramId}, function (err, doc){
There buy cialis pharmacy wouroud.com is also a solution provided for customers interested in child tracking throughout Canada. Gullible customers spend see these guys cialis no prescription thousands of dollars on products whose efficacy has not been validated, unlike Life enhancement products abet your body's natural staying power, improve satisfaction with sexual intercourse and make it more fulfilling, help in maintaining normal erectile function, boost libido and energy levels naturally. Symptoms In the dry macular degeneration, the general symptoms of buy cheap tadalafil  the patients are: Erectile dysfunctions Lack of sexual instinctsThese both problems are effectively cured by the acupuncture or not. Your affirmations will get  purchase generic viagra a boost and at the same time reduces hunger feelings. 			underscore.each(doc.comments, function(value){
				var temp = moment(value.timestamp).fromNow();
				value.timestamp  = temp;		
			});	
			res.json(doc);	
		});
	});
});