1

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?

7 Answers 7

3

Try moment.js

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

Sign up to request clarification or add additional context in comments.

Comments

2

You have a lot of ways to do this:

1 Comment

SELECT DATE_FORMAT(ent_date, '%W %M %d %Y') FROM tblentry
0

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...?

1 Comment

PHP. I wouldn't use ASP if it meant the cure to cancer.
0

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');

Comments

0

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');

Comments

0

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.

Comments

0

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)

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.