I recently wanted to format a date in node.js like this: dd/MM/yyyy hh:ss. There may be a better way to do it, but I decided to create a dateFormatter.js module so that I could see how to reuse some javascript code in node.js.
My first thought was that I needed to expose a new javascript object DateFormatter with the method format(d). Makes sense right?
So I set off by creating a file named dateFormatter.js that looked something like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DateFormatter = { | |
format: function(d) { | |
var d_date = d.getDate(); | |
var d_month = d.getMonth(); | |
d_month++; | |
var d_year = d.getFullYear(); | |
var a_p = ''; | |
var d_hour = d.getHours(); | |
if (d_hour < 12) | |
{ | |
a_p = 'AM'; | |
} | |
else | |
{ | |
a_p = 'PM'; | |
} | |
if (d_hour == 0) | |
{ | |
d_hour = 12; | |
} | |
if (d_hour > 12) | |
{ | |
d_hour = d_hour - 12; | |
} | |
var d_min = d.getMinutes(); | |
d_min = d_min + ''; | |
if (d_min.length == 1) | |
{ | |
d_min = '0' + d_min; | |
} | |
return d_date + '/' + d_month + '/' + d_year + ' ' + d_hour + ' : ' + d_min + ' ' + a_p; | |
} | |
} | |
exports.DateFormatter = new DateFormatter(); |
And no, it didn't run at all. Like I said, not great at using javascript in an OOP way!
What I didn't understand was that modules in node.js are wrapped as objects for you.
So by using the exports. I was really defining the public interface of my object. So, I changed the code to this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.format = function (d) { | |
var d_date = d.getDate(); | |
var d_month = d.getMonth(); | |
d_month++; | |
var d_year = d.getFullYear(); | |
var a_p = ''; | |
var d_hour = d.getHours(); | |
if (d_hour < 12) | |
{ | |
a_p = 'AM'; | |
} | |
else | |
{ | |
a_p = 'PM'; | |
} | |
if (d_hour == 0) | |
{ | |
d_hour = 12; | |
} | |
if (d_hour > 12) | |
{ | |
d_hour = d_hour - 12; | |
} | |
var d_min = d.getMinutes(); | |
d_min = d_min + ''; | |
if (d_min.length == 1) | |
{ | |
d_min = '0' + d_min; | |
} | |
return d_date + '/' + d_month + '/' + d_year + ' ' + d_hour + ':' + d_min + ' ' + a_p; | |
}; |
And then I reference and use it like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dateFormatter = require('../utilities/dateFormatter.js'); | |
app.get('/page', function(req, res){ | |
var d = new Date(); | |
console.log(dateFormatter.format(d)); | |
// prints out 25/2/2012 1:06 AM | |
... |
At the moment, this works for me.