如何将Json月份格式从数字改为名称?

时间:2015-06-23 23:38:18

标签: javascript html json json-api

我从Wordpress博客中提取了一些帖子,我想更改Json脚本的日期格式,而不是“2015-06-18 08:24:10”更改为“7月18日”

我正在使用Json api Wordpress插件和一个Html文件到cms中, JSon

            <script type="text/javascript">
var MYBLOG_LIMIT = 3;
var MYWRAPPER_CLASS = 'homeblog';

var WP={open:function(b){var a={posts:function(){var d=MYBLOG_LIMIT;var e=0;var c={all:function(g){var f=b+"/api/get_recent_posts/";f+="?count="+d+"&page="+e+"&callback=?";jQuery.getJSON(f,function(l){var k=l.posts;for(var j=0;j<k.length;j++){var h=k[j];h.createComment=function(i,m){i.postId=h.id;a.comments().create(i,m)}}g(k)})},findBySlug:function(f,h){var g=b+"/api/get_post/";g+="?slug="+f+"&callback=?";jQuery.getJSON(g,function(i){h(i.post)})},limit:function(f){d=f;return c},page:function(f){e=f;return c}};return c},pages:function(){var c={findBySlug:function(d,f){var e=b+"/api/get_page/";e+="?slug="+d+"&callback=?";jQuery.getJSON(e,function(g){f(g.page)})}};return c},categories:function(){var c={all:function(e){var d=b+"/api/get_category_index/";d+="?callback=?";jQuery.getJSON(d,function(f){e(f.categories)})}};return c},tags:function(){var c={all:function(e){var d=b+"/api/get_tag_index/";d+="?callback=?";jQuery.getJSON(d,function(f){e(f.tags)})}};return c},comments:function(){var c={create:function(f,e){var d=b+"/api/submit_comment/";d+="?post_id="+f.postId+"&name="+f.name+"&email="+f.email+"&content="+f.content+"&callback=?";jQuery.getJSON(d,function(g){e(g)})}};return c}};return a}};

var blog = WP.open('http://blog.tavorro.com/');
blog.posts().all(function(posts){
  for(var i = 0; i < posts.length; i++){
    jQuery('.'+MYWRAPPER_CLASS).append(function(){
      return (posts[i].thumbnail) ? '<div class="coll"><div class="date"><span>'+posts[i].date+'</span></div><a class="lastpost_title" href="'+posts[i].url+'"><h4>'+posts[i].title+'</h4></a><div class="blog-post"><p>'+posts[i].excerpt+'<p/></div></a><div class="link-read"><a href="'+posts[i].url+'">Read More</a></div>' : '<a href="'+posts[i].url+'"><h4>'+posts[i].title+'</h4></div>';

    });
  }
});
</script>   

请帮助我,欢迎任何建议!

以下是应更改日期的网站website

1 个答案:

答案 0 :(得分:0)

返回的日期是字符串,因此如果没有适当的本地化格式化程序,您最好的选择是执行以下操作:

var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
blog.posts().all(function(posts){
  for(var i = 0; i < posts.length; i++){
    jQuery('.'+MYWRAPPER_CLASS).append(function(){
      var dt = new Date(posts[i].date);
      return (posts[i].thumbnail) ? '<div class="coll"><div class="date"><span>'+months[dt.getMonth()]+dt.getDate()+'</span></div><a class="lastpost_title" href="'+posts[i].url+'"><h4>'+posts[i].title+'</h4></a><div class="blog-post"><p>'+posts[i].excerpt+'<p/></div></a><div class="link-read"><a href="'+posts[i].url+'">Read More</a></div>' : '<a href="'+posts[i].url+'"><h4>'+posts[i].title+'</h4></div>';

    });
  }
});