suchislife
suchislife

Reputation: 1

JQuery format date from MySQL. How?

I have the following date format in a MySQL database and was wondering what would be the best method to reformat this date and output what I have in mind.

MySQL: 2012-04-12

Desired Output: Thursday, April 12 2012

Perhaps I could format it in the MySQL query? Or with Java script?

Could anyone provide a sample of both so I could learn?

Upvotes: 1

Views: 6755

Answers (7)

elboletaire
elboletaire

Reputation: 5367

You have a lot of ways to do this:

Upvotes: 2

t0s6i
t0s6i

Reputation: 171

Try moment.js

e.g. var day = moment("2012-04-12", "DDDD, MMMM DD YYYY");

Upvotes: 3

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79840

From javascript you can format your date using below function, DEMO

Note: The below function additionally displays sup for date (ex: 3rd, 12th e.t.c). Remove all the if..else if in below code if you don't want it.

function formatDate(d) {

    var d_names = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

    var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    var curr_day = d.getDay();
    var curr_date = d.getDate();
    var sup = "";
    if (curr_date == 1 || curr_date == 21 || curr_date == 31) {
        sup = "st";
    } else if (curr_date == 2 || curr_date == 22) {
        sup = "nd";
    } else if (curr_date == 3 || curr_date == 23) {
        sup = "rd";
    } else {
        sup = "th";
    }

    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();

    return  d_names[curr_day] + " " + m_names[curr_month] + " " + curr_date + sup + "  " + curr_year;

}

Reference: Javascript Date formatting (above is modified version of Format #5)

Upvotes: 0

Doc Roms
Doc Roms

Reputation: 3308

When you have an date with mysql, it's you receive it in Php no?

for optimize your performance, use the native php date function to convert your mysql date , it's easy:

read just the official PHP doc => http://php.net/manual/fr/function.date.php

Php treatments as more fast than javascript treatments.

Upvotes: 0

prodigitalson
prodigitalson

Reputation: 60413

The best approach is usually to format the value with the PHP side just before outputting it:

$date = DateTime::createFromFormat('Y-m-d', $row['the_date']);
echo $date->format('J, M d Y');

Upvotes: 0

steveukx
steveukx

Reputation: 4368

PHP has a convenient class for reading dates in a specific format:

http://www.php.net/manual/en/datetime.createfromformat.php

$result = mysql_query("select date_column from table_name limit 1");
$array = mysql_fetch_array($result);

print DateTime::createFromFormat('Y-m-d')->format('jS M Y');

Upvotes: 0

Yogurt The Wise
Yogurt The Wise

Reputation: 4499

For formatting in javascript using jquery, loo at this other post.

Best JavaScript Date Parser & Formatter?

I would probably format with the code actually pulling the date form MySql. What language are you using? php, asp, java...?

Upvotes: 0

Related Questions