Solved

jQUERY DATE FORMAT

Posted on 2013-01-19
6
1,972 Views
Last Modified: 2013-03-09
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?
0
Comment
Question by:JoeBo747
6 Comments
 
LVL 42

Accepted Solution

by:
David S. earned 250 total points
ID: 38797688
0
 
LVL 52

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 250 total points
ID: 38797719
You can just ust the toString() function I think and it will go to the local time zone
<script>
var date1 = new Date(); 
var date2=date1.toString();
$('div.time').text(date2);
</script>
 <div class="time"></div>

Open in new window

and for reference some help below
http://docs.jquery.com/UI/Datepicker/formatDate
http://www.w3schools.com/jsref/jsref_obj_date.asp
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 38797757
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/
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 38798828
<!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.
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 38798846
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

0
 
LVL 1

Author Comment

by:JoeBo747
ID: 38798950
Sorry I have not abandoned this post I have been travelling and will catch up tomorrow.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now