Link to home
Start Free TrialLog in
Avatar of JoeBo747
JoeBo747

asked on

jQUERY DATE FORMAT

Hi,
I am new to jquery and need to use a date formatted in the  UK style I am using  var date1 = new Date(); to obtain the date and have attempted to convert the date returned 'Sat Jan 19 2013 16:52:39 GMT+0100 (W. Europe Standard Time)'  to the format 'dd/mm/yy hh:mm' how to I achive this?
ASKER CERTIFIED SOLUTION
Avatar of David S.
David S.
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is the top link in google for this FAQ
http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript

it mentions moment.js which is the library to use if you don't already use jQuery UI
http://momentjs.com/
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
    </head>
    <body>
        <div id="date"></div>
        <script>
            
           ( function writeDate(){
            var date    = new Date(),
                month   = date.getMonth() + 1, // 0-11,
                day     = date.getDate(), 
                year    = date.getFullYear(),
                hour    = date.getHours(),
                minute  = date.getMinutes();
            
                year = year.toString().slice(2,4);
            
            
                var dateContainer = document.getElementById("date");
                dateContainer.innerHTML = day+"/"+month+"/"+year+" "+hour+":"+minute;
            })();
            </script>
    </body>
</html>

Open in new window

Do you want to include leading 0's?

In this example the script is at the bottom of the page so you don't need an onload event. If you want to put this script at the top of your page, you'll need a window.onload event.
here's a version with leading 0's

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta charset="UTF-8">
        
    </head>
    <body>
        <div id="date"></div>
        <script>
            
           ( function writeDate(){
            var date    = new Date(),
                month   = date.getMonth() + 1, // 0-11,
                day     = date.getDate(),
                year    = date.getFullYear(),
                hour    = date.getHours(),
                minute  = date.getMinutes();
            
                year = year.toString().slice(2,4);
                
                
                month = month < 10 ? "0"+month : month;
                minute = minute < 10 ? "0"+minute : minute;
            
                var dateContainer = document.getElementById("date");
                dateContainer.innerHTML = day+"/"+month+"/"+year+" "+hour+":"+minute;
            })();
            </script>
    </body>
</html>

Open in new window

Avatar of JoeBo747
JoeBo747

ASKER

Sorry I have not abandoned this post I have been travelling and will catch up tomorrow.