0

I want to change date format in json when i bind the result .

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 .

3
  • result['0']['start_time'] is stored data right Commented Jul 26, 2017 at 7:08
  • You just change date format for insert Commented Jul 26, 2017 at 7:09
  • we can not change in json , i tried like date('d-m-Y',strtotime(result['0']['start_time'])); i already tried this in other page and it work but if i use it in json it not work. Commented Jul 26, 2017 at 7:19

3 Answers 3

1

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());
Sign up to request clarification or add additional context in comments.

7 Comments

ok for time it work perfectly but now i want to show date like 25-07-2017 how can i get, i use d.getDate() but it only return 25(mean current date only)
i think result['0']['start_time'] has the date you want so can directly use that
no i use result['0']['start_time'] for time and result['0']['start_date'] for date ,i use two field for date and time in my table.
yes i use your answer for time ,and result['0']['start_date'] also return 2017-07-25 but i want d-m-Y format 25-07-2017 how i get
i follow your answer it only return year and i add likeconsole.log(d.getHours() + ':' + d.getMinutes() + 1 + '-' + d.getDate() + '-' + d.getMonth() + '-' + d.getFullYear()); but in place of month it return 6 but in my table it has 7
|
0

I suggest you try moment.js from https://momentjs.com/

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

Comments

0

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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.