Hello world, in this article, I am gonna show you, How To Get Short Date String In Jquery
In this post, I am gonna show you how we can get complete date without concatenating Date.getYear(), Date.getMonth(), and Date.getDay() each time.
So I will create an extension method in jquery that will return complete date(How simple :)) and I am going to use MM-DD-YYYY format, you can change it according to your need.
// Getting date with format mm/dd/yyyyy Date.prototype.mmddyyyy = function () { // adding 1 because getMonth() will always returns current month -1 var mm = (this.getMonth() + 1).toString(); var dd = this.getDate().toString(); var yyyy = this.getFullYear().toString(); return (mm[1] ? mm : "0" + mm[0]) + '/' + (dd[1] ? dd : "0" + dd[0]) + '/' + yyyy; };
As you can see I registered mmddyyyy function in jquery and in that function, I am first getting month, I you might noticed that there I am adding +1 the reason behind this because getMonth always returns current month – 1, means if current month is feb(2) then getMonth will return 1.
After getting month, I am getting day and year and at the last I am first checking if month or date having single value or not, means if month have 1 then I am concatenating 0 if month is 10, 11 and 12 then I am getting original text and I am doing same with day.
You can call that method by this
(new Date()).mmddyyyy();
Hope you’ll like this
Nice little piece of code. I might check back for that update! 😉
Glad you like this 🙂
WONDERFUL Post.thanks for share.