最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript convert string into Date with format (dd mmm yyyy) i.e. 01 Jun 2012 - Stack Overflow

programmeradmin4浏览0评论

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

Share Improve this question edited Apr 22, 2017 at 6:58 Donald Duck 8,89223 gold badges79 silver badges102 bronze badges asked Jul 3, 2013 at 10:27 Satinder singhSatinder singh 10.2k18 gold badges64 silver badges102 bronze badges 4
  • 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
Add a comment  | 

6 Answers 6

Reset to default 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

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.

发布评论

评论列表(0)

  1. 暂无评论