Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQUERY DATE FORMAT

Posted on 2013-01-19
6
Medium Priority
?
2,041 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 43

Accepted Solution

by:
David S. earned 750 total points
ID: 38797688
0
 
LVL 54

Assisted Solution

by:Scott Fell, EE MVE
Scott Fell,  EE MVE earned 750 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

971 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