简体   繁体   English

在json格式上更改日期格式

[英]change date format on json format

I want to change date format in json when i bind the result . 绑定结果时,我想更改json中的日期格式。

My script 我的剧本

$.ajax({
      type: "POST",
      async: false,
      url: "<?php echo site_url(); ?>admin/get_mail_contents",
      data: datastring,
      cache: false,
      success: function (result) {
          alert(result);
          var result = $.parseJSON(result);
          console.log(result.from);                                    
          $('#content').html('');
          $("#content").append('<div class="mail-toolbar clearfix"> </div><div class="divider"></div><div class="pad5A clearfix mrg10B"> <i class="glyph-icon icon-clock-o mrg5R"></i> ' + result['0']['start_time'] + ' , ' + result['0']['start_date'] + '</div></div> </div>');
      }
});

My actual out put is 15:37:00 , 2017-07-25 and i want to print 15:37 , 25-07-2017 in view how to change in json . 我的实际输出是2017年7月25日15:37:00,我想打印15:37,25-07-2017以查看如何更改json。

Try this 尝试这个

var d = new Date(result['0']['start_date'] +' '+ result['0']['start_time']);

console.log(d.getHours() + ':' + d.getMinutes());

console.log((d.getDate()) + '-' + (d.getMonth() + 1) + '-' +  d.getFullYear());

I suggest you try moment.js from https://momentjs.com/ 我建议您尝试从https://momentjs.com/获得的 moment.js

It's easy to use to transform your formatted date to another formatting 可以很容易地将格式日期转换为其他格式

Here's what I tried on my browser console 这是我在浏览器控制台上尝试过的

$.getScript("moment.js");
dt = new Date("15:37:00 , 2017-07-25");
moment(dt).format("HH:mm , DD-MM-YYYY");

and it will output as something like this: 它会像这样输出:

"15:37 , 25-07-2017"

Hope this helps 希望这可以帮助

Use split function to create array of your date then rearrange values then create string from that array 使用分割功能创建日期数组,然后重新排列值,然后从该数组创建字符串

 var date_array=result['0']['start-date'].split('-');
 var date_string=date_array[2].'-'.date_array[1].'-'.date_array[0];

Then use the date_string instead of result['0']['start_date'] 然后使用date_string而不是result ['0'] ['start_date']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM