This is my old home .. find me now at andyj.github.io

DateFormat in #nodejs
Posted:

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 Time function 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 https://github.com/felixge/node-dateformat'; // output to the browser res.writeHead(200, {'Content-Type': 'text/plain'}); res.end( textResponse ); }).listen(1337, "127.0.0.1"); // Tell the console when the server was started console.log('Server running on http://127.0.0.1:1337/ 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 github.com/felixge/node-dateformat

comments powered by Disqus
© AndyJarrett 2013 | RSS Feed