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
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
- 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 Commented Jul 3, 2013 at 10:42
- am not using any external js file, is it possible to do it without using any external file – Satinder singh Commented Jul 3, 2013 at 10:44
- Look at this: stackoverflow.com/questions/1056728/… – alun Commented Jul 3, 2013 at 10:47
- did u use this 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 Commented Jul 3, 2013 at 10:51
6 Answers
Reset to default 9Try 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
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
return $.datepicker.formatDate('dd-M-yy', new Date(dateVal)); //01-Dec-2014
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.-
"format" is not a standard method of Date object
dt.format("dd MMM yyyy")
Use capital letters.