5

Possible Duplicate:
Formatting a date in JavaScript

I need to show a current date in format like this (some examples):

sep 10, 2012
nov 5, 2012

and so on.

Using javascript I get a current date object

var date  = new Date();

what I need to do next?

Community
  • 1
  • 1
  • 4
    Have you seen [Formatting a date in javascript](http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript)? – Kos Nov 22 '12 at 12:52
  • 4
    Please use the search feature before asking a question. – Cerbrus Nov 22 '12 at 12:53

5 Answers5

2

you can use this.

function dateTest(){
  var d =new Date();
 var month_name=new Array(12);
 month_name[0]="Jan"
 month_name[1]="Feb"
 month_name[2]="Mar"
 month_name[3]="Apr"
 month_name[4]="May"
 month_name[5]="Jun"
 month_name[6]="Jul"
 month_name[7]="Aug"
 month_name[8]="Sep"
 month_name[9]="Oct"
 month_name[10]="Nov"     
 month_name[11]="Dec"
 alert(month_name[d.getMonth()]+" "+d.getDate()+" , "+d.getFullYear());
 }
Srinivas B
  • 1,821
  • 4
  • 17
  • 35
1

Use dateFormat lib available in following link http://stevenlevithan.com/assets/misc/date.format.js

Refer this article for formatting js using above lib. http://blog.stevenlevithan.com/archives/date-time-format

Edit: If you cant use lib then

var d = new Date();

var curr_date = d.getDate();

var curr_month = d.getMonth();

var curr_year = d.getFullYear();

var formattedDate = curr_date + " " + curr_month + ", " + curr_year;

Sandeep Manne
  • 6,030
  • 5
  • 39
  • 55
  • There's [moment.js](http://stackoverflow.com/a/10119138/399317) too, it got lots of upvotes on the other question. – Kos Nov 22 '12 at 12:55
  • I cannot use any third party libraries –  Nov 22 '12 at 12:56
1

You can use the getMonth (including some switch/case for the text), the getDate and the getFullYear methods to build your string.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/prototype#Methods

looper
  • 1,929
  • 23
  • 42
0

EDITED

You can use this function in your code :

function getFormattedDate(input){
   var pattern=/(.*?)\/(.*?)\/(.*?)$/;
   var result = input.replace(pattern,function(match,p1,p2,p3){
   var months=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Dec'];
   return (months[(p1-1)]+" "+p2<10?"0"+p2:p2)+" "+p3;
});
alert(result);
}

And for ref. you can call this function directly like this:

getFormattedDate("11/18/2013");

OR you can use this code, too.

var date  = new Date();
dateFormat(date,"mediumDate");

you can also find further different formats here.

Bhavik Patel
  • 752
  • 1
  • 5
  • 20
0

Try this

function getCurrentDate(){
var now=new Date();
var date=now.getDate();
var year=now.getFullYear();
var months=new Array('jan', 'feb', 'mar' ... 'dec');
var month=months[now.getMonth()]

return month + ' ' + date + ', ' + year;
}
Ram G Athreya
  • 4,892
  • 6
  • 25
  • 57