2017 S. Elm Pl Suite 105 Broken Arrow, OK

Top

JavaScript Dates

JavaScript Dates

JavaScript Dates

JavaScript gives you a lot of flexibility when it comes to date functionality. You can easily pull out any segment individually with just a simple function call. However if you want to format a date, you’ll either need to use the built-in format function, or do a long string concatenation which makes your code look messy.

In my spare time, I made a Server Side JavaScript function for Classic ASP. The source and an example is posted on my personal blog. This function allows you to format dates similar to how PHP’s Date function works. I decided it would be amazing if I could just go ahead and tweak this to work with JavaScript on the client side, so I went ahead and modified my current script to extend the JavaScript date object. Here is a quick peak at how to use this.

console.log(new Date(2012, 1, 25, 10).formatDate('Y-m-d H:i:s'));
// 2012-02-25 10:00:00
console.log(new Date().formatDate('l, F jS, Y'));
// Monday, February 27th, 2012
console.log(new Date().formatDate('r'));
// Mon, 27 Feb 2012 14:03:40 -0600 

Download formatDate.js

(Source: Skynet Solutions)

By Blaine Schmeisser