6

I am getting a string variable having date in format 6/1/2012, I want to convert it into 01 Jun 2012 . JS FIDDLE DEMO

Code I tried:

var t_sdate="6/1/2012";                  
var sptdate = String(t_sdate).split("/");
var myMonth = sptdate[0];
var myDay = sptdate[1];
var myYear = sptdate[2];
var combineDatestr = myYear + "/" + myMonth + "/" + myDay;

var dt = new Date(combineDatestr);
var formatedDate= dt.format("dd mmm yyyy")
alert(formatedDate);

Getting output as 01 000 2012, required as 01 Jun 2012

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
  • Uncaught TypeError: Object [object Date] has no method 'format' was the error logged in console. The code you have will work if you include dateformat-js plugin in your code. – Monie corleone Jul 03 '13 at 10:42
  • am not using any external js file, is it possible to do it without using any external file – Satinder singh Jul 03 '13 at 10:44
  • Look at this: http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript?rq=1 – alun Jul 03 '13 at 10:47
  • did u use this http://blog.stevenlevithan.com/archives/date-time-format ? u sure u dwonloaded the date.format.js file and set it up properly before u started coding? – manraj82 Jul 03 '13 at 10:51

6 Answers6

9

Try this:

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','Nov','Dec'];
        return (p2<10?"0"+p2:p2) + " " + months[(p1-1)] + " " + p3;
    });

    alert(result);
}

getFormattedDate("6/1/2013");

Jsfiddle demo

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
Chickenrice
  • 5,727
  • 2
  • 22
  • 21
2

Since other users already mentioned that "format" is not a standard method of Date object. You can do it without using any format method (even if there exist any)

var t_sdate = "6/1/2012";
var sptdate = String(t_sdate).split("/");
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
var myMonth = sptdate[0];
var myDay = sptdate[1];
var myYear = sptdate[2];
var combineDatestr = myDay + " " + months[myMonth - 1] + " " + myYear;

alert(combineDatestr);

JsFiddle Demo

sohaiby
  • 1,168
  • 3
  • 24
  • 39
1
return $.datepicker.formatDate('dd-M-yy', new Date(dateVal)); //01-Dec-2014
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
DhMi
  • 11
  • 1
  • 3
    Hello, and welcome to Stack Overflow. In general, if the tags don't include `jquery` (or another library), the answers should refrain from using libraries to solve the problem (or they can suggest that using a library would be easier). Here, you just assume the OP is using jQueryUI; the answer would have been more useful had you suggested that jQueryUI has this cool functionality, but no library answers are still preferred (especially given OP's question comment). Also take a look at Stack Overflow formatting help: indent four characters for code blocks (or put in backticks for inline code). – Amadan Dec 01 '14 at 08:07
0

You may want to use javascript Intl https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat

The following example will show something like Nov 02, 2017

console.log(new Intl.DateTimeFormat('en-EN', { year: 'numeric', month: 'short', day: 'numeric' }).format(new Date()));

Milton.-

Milton
  • 928
  • 1
  • 10
  • 22
-1

"format" is not a standard method of Date object

guy777
  • 222
  • 1
  • 14
-4
dt.format("dd MMM yyyy")

Use capital letters.

TobiMcNamobi
  • 4,687
  • 3
  • 33
  • 52
satish
  • 1