DateFormat in #nodejs

Adding some DateFormat functions to Node ended up being quite simple

$ mkdir testDateFormat && testDateFormat$ npm install dateformat $ subl app.js

For this I installed it locally, you can always add -g to add it locally. The final line opens up a new file called app.js in Sublime Text 2 for me. In there we are going to create a simple web app (cause thats what you do with Node.js apparently) and quickly return the date to a Hello World program.

var http = require('http');// requre the dateFormat var dateFormat = require('dateFormat')// Create a function to return the current Date and Timefunction getDateTime(){ var now = new Date(); return dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");}http.createServer(function (req, res) { // Add some output to the browser including date and time var textResponse = "Hello world @ " + getDateTime() + '\\n\\nCheck out more at'; // output to the browser res.writeHead(200, {'Content-Type': 'text/plain'}); res.end( textResponse ); }).listen(1337, "");// Tell the console when the server was startedconsole.log('Server running on Started @ ' + getDateTime());

Once you've saved app.js call $ node app.js from the Terminal and go to http://localhost:1337.

This only scratches the service of what it can do, check out more at

comments powered by Disqus