-4

I am retrieving a date from MySQL database, which is in the format of 2015-09-14 17:15:24. But I want to convert it to 14 Sep, 2015 | 4:15 pm. I've tried one of the Javascript date library called date.js but I didn't get desired result. Is there any code to do this or any tiny Javascript library to convert it?

My Code is :

var Time = React.createClass({

    render: function() {

        var t = this.props.time.split(/[- :]/);

        // Apply each element to the Date function
        var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);

        console.log(d);

        return (
            <div>
                <span className="grey-text text-lighten-1">
                    <span className="glyphicon glyphicon-time" aria-hidden="true"></span> {d}
                </span>
            </div>
        );
    }

});

Thank You..

Swapnil Bhikule
  • 515
  • 7
  • 23

2 Answers2

0

try this.. momentjs.com

var moment = require('moment');
var yourDateInput = '2015-09-14 17:15:24';
var d = new moment(yourDateInput);
console.log(d.format('DD MMM, YYYY | h:mm a'));     // 14 Sep, 2015 | 4:15 pm
g2000
  • 480
  • 3
  • 8
0

Well not exactly but I figure out a way to convert mysql timestamp to normal human readable date. Not in javascript but in mysql, so I can directly pass mysql data to js and my js just have to display data without any manipulation. My code is:

DATE_FORMAT(created_at, '%D %M, %Y   %l:%i %p')

Thanks for suggesting codes and other stuff.

Swapnil Bhikule
  • 515
  • 7
  • 23